description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
def potions(n): lst = [int(i) for i in input().split()] l = [] for potion in lst: l.append(potion) if potion < 0 and sum(l) < 0: l = sorted(l) l.pop(0) print(len(l)) n = int(input()) potions(n)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
n = int(input()) ls = list(map(int, input().split())) drk, ans, hp = [], 0, 0 for i in ls: if i >= 0: ans, hp = ans + 1, hp + i elif hp + sum(drk) + i >= 0: drk.append(i) else: try: temp = min(drk) except ValueError: continue if temp < i: drk.remove(temp) drk.append(i) print(ans + len(drk))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
n = int(input()) li = list(map(int, input().split())) q = [] c = 0 v = 0 for i in range(0, n): v += li[i] c += 1 if li[i] < 0: q.append(li[i]) q.sort() if v < 0: v -= q[0] q.sort() q.pop(0) q.sort() c = c - 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
def solve(n, a): count = health = 0 i = 0 while i < len(a): health += a[i] count += 1 if health < 0: min_dam = min(a[: i + 1]) a.remove(min_dam) health -= min_dam count -= 1 continue i += 1 return count n = int(input()) a = list(map(int, input().strip().split()))[:n] print(solve(n, a))
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
n = int(input()) a = [int(x) for x in input().split()] s = 0 k = 0 b = [] for i in range(0, n): s = s + a[i] k = k + 1 if a[i] < 0: b.append(a[i]) while s < 0: p = min(b) s = s - p k = k - 1 b.remove(p) print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
n = int(input()) a = list(map(lambda x: int(x), input().split())) s = 0 ans = [] for i in range(n): if s + a[i] >= 0: s += a[i] ans.append(a[i]) ans.sort() elif ans and ans[0] < a[i]: s += a[i] - ans[0] ans.pop(0) ans.append(a[i]) ans.sort() print(len(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
def func(): n = int(input()) l = list(map(int, input().split())) pois = 0 health = 0 neg_pos = [] for potion in l: if potion >= 0: pois += 1 health += potion elif potion + health >= 0: pois += 1 health += potion neg_pos.append(potion) elif neg_pos: mn = min(neg_pos) if potion > mn: health = health - mn + potion neg_pos.remove(mn) neg_pos.append(potion) return pois t = 1 for i in range(t): print(func())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
n = int(input()) arr = list(map(int, input().split())) dp = [] for i in arr: dp.append(i) if i >= 0: continue dp.sort() if sum(dp) < 0: dp.pop(0) print(len(dp))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
a = int(input()) arr = [int(arr) for arr in input().split()] total = 0 count = 0 neg = [] for i in arr: total = total + i count = count + 1 if i < 0: neg.append(i) if total < 0: total = total - min(neg) neg.remove(min(neg)) count = count - 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
def solve(l): c, s = 0, 0 k = [] for i in range(len(l)): if l[i] >= 0: s = s + l[i] c = c + 1 else: k.append(l[i]) s = s + l[i] c = c + 1 while s < 0: k.sort() s = s - k[0] k.pop(0) c = c - 1 return c n = int(input()) l = list(map(int, input().split())) print(solve(l))
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
def put_into_list(num: int, arr: list): if not arr: arr.append(num) return for i in range(len(arr)): if arr[i] > num: arr.insert(i, num) return arr.append(num) def poisons_simple(n: int, dr: list): health = 0 amount_pois = 0 neg_pois = [] for i in range(n): if dr[i] >= 0: amount_pois += 1 health += dr[i] continue if health + dr[i] >= 0: amount_pois += 1 health += dr[i] put_into_list(dr[i], neg_pois) continue if neg_pois and dr[i] > neg_pois[0]: health -= neg_pois[0] health += dr[i] neg_pois.pop(0) put_into_list(dr[i], neg_pois) return amount_pois n = int(input()) arr = list(map(int, input().split(" "))) print(poisons_simple(n, arr))
FUNC_DEF VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
n = int(input()) lis = list(map(int, input().split())) ans = 0 arr = list() for i in lis: ans += i arr.append(i) arr.sort() while ans < 0: ans -= arr[0] arr.pop(0) print(len(arr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
n = int(input()) s = count = 0 taken = [] ncount = 0 for i in list(map(int, input().split())): if i >= 0: count += 1 ncount += 1 s += i elif s + i >= 0: s += i count += 1 taken.append(i) elif len(taken) > 0: v = min(taken) if s + i - v >= 0 and v < i: s += i - v taken.append(i) del taken[taken.index(v)] else: pass print(ncount + len(taken))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
def poisons(n: int, dr: list): health = 0 amount_pois = 0 neg_pois = [] for i in range(n): if dr[i] >= 0: amount_pois += 1 health += dr[i] continue if health + dr[i] >= 0: amount_pois += 1 health += dr[i] neg_pois.append(dr[i]) continue if neg_pois: cur_min = min(neg_pois) if dr[i] > cur_min: health -= cur_min health += dr[i] neg_pois.remove(cur_min) neg_pois.append(dr[i]) return amount_pois n = int(input()) arr = list(map(int, input().split(" "))) print(poisons(n, arr))
FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β€” the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
input() x = map(int, input().split()) m = 0 m1 = [] c = 0 for i in x: if m + i >= 0: if i < 0: m1 += [i] elif m1: if i > min(m1): m = m - min(m1) m1.remove(min(m1)) m1.append(i) c -= 1 if m + i >= 0: m += i c += 1 print(c)
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR LIST VAR IF VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
import sys input = sys.stdin.readline n, x = map(int, input().split()) a = [int(x) for x in input().split()] dp = [[0, 0, 0, 0] for i in range(n)] for i in range(n): dp[i][0] = max(a[i], dp[i - 1][0] + a[i]) dp[i][1] = max(a[i] * x, dp[i - 1][0] + a[i] * x) dp[i][2] = max(dp[i - 1][1], dp[i - 1][2]) + a[i] * x dp[i][3] = max(dp[i - 1][3], max(dp[i - 1][2], dp[i - 1][1])) + a[i] maxi = 0 for i in range(n): maxi = max(maxi, max(dp[i])) print(maxi if n > 1 else max(max(a[0] * x, a[0]), 0))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def printarr(dp): for i in dp: print(*i) n, m = map(int, input().split()) a = [0] + list(map(int, input().split())) dp = [[0, 0, 0] for i in range(n + 1)] ma = -1 for i in range(1, n + 1): dp[i][0] = max(dp[i - 1][0] + a[i], 0) dp[i][1] = max(dp[i - 1][1] + a[i] * m, dp[i - 1][0] + a[i] * m) dp[i][2] = max(dp[i - 1][2] + a[i], a[i] + dp[i - 1][1]) ma = max(dp[i][0], dp[i][1], dp[i][2], ma) print(ma)
FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) arr = [int(x) for x in input().split()] dp = [[(0) for _ in range(n)] for _ in range(3)] dp[0][0] = max(arr[0], 0) dp[1][0] = max(arr[0] * x, 0) dp[2][0] = max(arr[0], 0) answer = max(dp[0][0], dp[1][0], dp[2][0]) for i in range(1, n): dp[0][i] = max(dp[0][i - 1] + arr[i], arr[i], 0) dp[1][i] = max(dp[0][i - 1] + arr[i] * x, dp[1][i - 1] + arr[i] * x, arr[i] * x, 0) dp[2][i] = max(dp[1][i - 1] + arr[i], dp[2][i - 1] + arr[i], arr[i], 0) answer = max(answer, dp[0][i], dp[1][i], dp[2][i]) print(answer)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, m = map(int, input().split()) lis = list(map(int, input().split())) pref = suff = main = ans = 0 for i in lis: pref = max(pref + i, 0) suff = max(suff + i * m, pref) main = max(main + i, suff) ans = max(ans, main) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
import sys input = sys.stdin.readline n, x = list(map(int, input().split())) A = list(map(int, input().split())) SUM = [0] for a in A: SUM.append(SUM[-1] + a) MAXLIST = [SUM[0]] MINLIST = [SUM[0]] for i in range(1, n + 1): MAXLIST.append(max(MAXLIST[-1], SUM[i])) MINLIST.append(min(MINLIST[-1], SUM[i])) MAXLIST_INV = [SUM[-1]] MINLIST_INV = [SUM[-1]] for i in range(n - 1, -1, -1): MAXLIST_INV.append(max(MAXLIST_INV[-1], SUM[i])) MINLIST_INV.append(min(MINLIST_INV[-1], SUM[i])) MAXLIST_INV = MAXLIST_INV[::-1] MINLIST_INV = MINLIST_INV[::-1] if x > 0: ANS = 0 for i in range(n + 1): base = SUM[i] MINUS = MINLIST[i] ANS = max(ANS, (base - MINUS) * x) print(ANS) else: ANS = 0 MAX = 0 MIN = 0 MINUS = 0 NOWMINUS = 0 for i in range(n + 1): base = SUM[i] PLUS = MAXLIST_INV[i] ANS = max(ANS, NOWMINUS + PLUS - base + base * x) MIN = min(MIN, SUM[i]) if NOWMINUS <= SUM[i] - MIN + SUM[i] * -x: NOWMINUS = SUM[i] - MIN + SUM[i] * -x MAX = SUM[i] print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR 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 IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
N, X = map(int, input().split()) a = list(map(int, input().split())) dp = [([0] * 3) for i in range(N + 1)] ans = 0 for i in range(N): dp[i + 1][0] = max(0, dp[i][0] + a[i]) dp[i + 1][1] = max(0, dp[i][1] + a[i] * X, dp[i][0] + a[i] * X) dp[i + 1][2] = max(0, dp[i][1] + a[i], dp[i][2] + a[i]) ans = max(ans, dp[i + 1][0], dp[i + 1][1], dp[i + 1][2]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) a = list(map(int, input().split())) inf = -(2**64) dp = [[[inf for _ in range(3)] for _ in range(3)] for _ in range(n + 1)] dp[0][0][0] = 0 for i in range(n + 1): for j in range(3): for k in range(3): if k < 2: dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k]) if j < 2: dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]) if i < n: dp[i + 1][j][k] = max( dp[i + 1][j][k], dp[i][j][k] + (a[i] if j == 1 else 0) * int(x if k == 1 else 1), ) print(dp[n][2][2])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def find(A, x): maxi, c1, c2, c3 = 0, 0, 0, 0 for i in range(0, len(A)): c11 = max([c1, 0]) + A[i] c22 = max([c1, c2, 0]) + A[i] * x c33 = max([c2, c3, 0]) + A[i] c1, c2, c3 = c11, c22, c33 maxi = max([maxi, c1, c2, c3]) return maxi inp = lambda cast=int: list(map(cast, input().split())) n, x = inp() A = [0] + inp() print(find(A, x))
FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR LIST VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR LIST VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
l = input().split() n = int(l[0]) x = int(l[1]) l = input().split() li = [int(i) for i in l] right = [(0) for i in range(n)] left = [(0) for i in range(n)] pref = [(0) for i in range(n)] pref[0] = li[0] for i in range(1, n): left[i] = max(0, li[i - 1] + left[i - 1]) pref[i] = pref[i - 1] + li[i] for i in range(n - 2, -1, -1): right[i] = max(0, li[i + 1] + right[i + 1]) maxleft = 0 ans = 0 for i in range(n): if i == 0: maxleft = max(maxleft, left[i]) else: maxleft = max(maxleft, left[i] - x * pref[i - 1]) ans = max(ans, right[i] + x * pref[i] + maxleft) dp = [(0) for i in range(n)] dp[0] = li[0] for i in range(1, n): dp[i] = max(dp[i - 1] + li[i], li[i]) print(max(max(dp), ans))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) arr = [int(x) for x in input().split()] dp = [[(0) for i in range(3)] for j in range(n)] for i in range(n): dp[i][0] = max(0, arr[i]) dp[i][1] = max(0, arr[i]) dp[i][2] = max(0, arr[i] * x) if i == 0: continue dp[i][0] = max(dp[i][0], arr[i] + dp[i - 1][0]) dp[i][1] = max(dp[i][1], arr[i] + dp[i - 1][1], arr[i] + dp[i - 1][2]) dp[i][2] = max(dp[i][2], dp[i - 1][2] + arr[i] * x, dp[i - 1][0] + arr[i] * x) ans = 0 for i in range(n): ans = max(ans, dp[i][0], dp[i][1], dp[i][2]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) l = list(map(int, input().split())) not_used = [(0) for k in range(n + 1)] current = [(0) for k in range(n + 1)] used = [(0) for k in range(n + 1)] globalMax = 0 for k in range(n): not_used[k + 1] = max(not_used[k], 0) + l[k] current[k + 1] = max(max(not_used[k], current[k]), 0) + l[k] * x used[k + 1] = max(max(current[k], used[k]), 0) + l[k] globalMax = max(max(globalMax, used[k + 1]), max(current[k + 1], not_used[k + 1])) print(globalMax)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
d1, d2, d3, d4 = 0, 0, 0, 0 e1, e2, e3, e4 = 0, 0, 0, 0 n, x = map(int, input().split()) A = list(map(int, input().split())) + [0] for a in A: e1 = max(a, d1 + a) e2 = max(x * a, d1 + x * a, d2 + x * a) e3 = max(e1, d2 + a, d3 + a) e4 = max(d1, d2, d3, d4, a) d1, d2, d3, d4 = e1, e2, e3, e4 print(d4)
ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) a = list(map(int, input().split())) def solve(n, x, a): s = [a[0]] * n left = [max(a[0], 0)] * n right = [max(a[-1], 0)] * n for i in range(1, n): left[i] = max(left[i - 1] + a[i], a[i], 0) s[i] = s[i - 1] + a[i] for i in range(n - 2, -1, -1): right[i] = max(right[i + 1] + a[i], a[i], 0) res = c = 0 for r in range(n): if r < n - 1: res = max(res, right[r + 1] + x * s[r] + c) else: res = max(res, x * s[r] + c) res = max(res, left[r]) c = max(c, left[r] - x * s[r]) return res print(solve(n, x, a))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
N, X = map(int, input().split()) ans = 0 for i, x in enumerate(map(int, input().split())): if i == 0: dp = max(0, x), max(0, x * X), max(0, x) else: dp = ( max(0, dp[0] + x), max(0, dp[1] + x * X, dp[0] + x * X), max(0, dp[2] + x, dp[1] + x), ) ans = max(ans, dp[0], dp[1], dp[2]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
N, X = map(int, input().split()) A = [int(a) for a in input().split()] dp = [([0] * 4) for _ in range(N + 1)] for i in range(1, N + 1): dp[i][0] = max(dp[i - 1][0] + A[i - 1], 0) dp[i][1] = max(dp[i - 1][1] + A[i - 1] * X, dp[i][0]) dp[i][2] = max(dp[i - 1][2] + A[i - 1], dp[i][1]) dp[i][3] = max(dp[i - 1][3], dp[i][2]) print(dp[N][3])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def maxBeauty(): dp = [[(0) for i in range(3)] for j in range(n)] dp[0][0] = max(a[0], 0) dp[0][1] = max(a[0] * x, 0) dp[0][2] = max(a[0], 0) ans = 0 ans = max([ans, dp[0][0], dp[0][1], dp[0][2]]) for i in range(1, n): dp[i][0] = max(dp[i - 1][0] + a[i], 0) dp[i][1] = max(max(dp[i - 1][0], dp[i - 1][1]) + a[i] * x, 0) dp[i][2] = max(max(dp[i - 1][1], dp[i - 1][2]) + a[i], 0) ans = max([ans, dp[i][0], dp[i][1], dp[i][2]]) return ans lin = [int(i) for i in input().split()] n = lin[0] x = lin[1] a = [int(i) for i in input().split()] print(maxBeauty())
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, m = (int(x) for x in input().split()) multipliers = [1] * 3 multipliers[1] = m arr = [int(x) for x in input().split()] dp = [[(0) for j in range(3)] for i in range(n + 1)] for i in range(n): for j in range(len(multipliers)): dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + arr[i] * multipliers[j]) if j + 1 < len(multipliers): dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]) print(max(map(max, dp)))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) A = list(map(int, input().split())) DP = [([0] * 3) for _ in range(n + 1)] ans = 0 for i in range(1, n + 1): DP[i][0] = max(DP[i - 1][0] + A[i - 1], A[i - 1]) DP[i][1] = max( DP[i - 1][0] + A[i - 1] * x, DP[i - 1][1] + A[i - 1] * x, A[i - 1] * x ) DP[i][2] = max(DP[i - 1][1] + A[i - 1], DP[i - 1][2] + A[i - 1], A[i - 1]) ans = max(ans, max(DP[i])) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
import sys input = sys.stdin.readline n, x = map(int, input().split()) a = list(map(int, input().split())) INF = 10**18 dp = [[([-INF] * 3) for i in range(3)] for j in range(n + 1)] dp[0][0][0] = 0 for i in range(n + 1): for j in range(3): for k in range(3): if k < 2: dp[i][j][k + 1] = max(dp[i][j][k], dp[i][j][k + 1]) if j < 2: dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]) if i < n: if k == 1: dp[i + 1][j][k] = max( dp[i + 1][j][k], dp[i][j][k] + a[i] * (j == 1) * x ) else: dp[i + 1][j][k] = max( dp[i + 1][j][k], dp[i][j][k] + a[i] * (j == 1) * 1 ) print(dp[n][2][2])
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) lis = list(map(int, input().split())) dp = dict() mx = mx = float("-inf") dp[0, 1, 0] = lis[0] dp[0, 1, 1] = x * lis[0] dp[0, 0, 0] = 0 mx = max(mx, max(dp[0, 1, 0], dp[0, 1, 1])) for i in range(1, n): dp[i, 1, 1] = max(x * lis[i], x * lis[i] + max(dp[i - 1, 1, 0], dp[i - 1, 1, 1])) dp[i, 1, 0] = max(lis[i], lis[i] + dp[i - 1, 1, 0]) dp[i, 0, 0] = max(lis[i], lis[i] + max(dp[i - 1, 0, 0], dp[i - 1, 1, 1])) mx = max(mx, dp[i, 1, 1], dp[i, 1, 0], dp[i, 0, 0]) print(0 if mx < 0 else mx)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) dp1 = [0] * n dp2 = [0] * n dp0 = [0] * n ans = 0 v = [int(i) for i in input().split()] dp0[0] = max(0, v[0]) dp1[0] = v[0] * x i = 0 ans = max(ans, dp1[i], dp2[i], dp0[i]) for i in range(1, n): dp0[i] = max(0, dp0[i - 1] + v[i]) dp1[i] = max(dp1[i - 1] + v[i] * x, dp0[i - 1] + v[i] * x) dp2[i] = max(dp1[i - 1] + v[i], dp2[i - 1] + v[i]) ans = max(ans, dp1[i], dp2[i], dp0[i]) print(ans)
ASSIGN VAR 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def solve(): n, x = list(map(int, input().split())) a = [0] + list(map(int, input().split())) max_val = 0 dp1 = [0] * (n + 1) for i in range(1, n + 1): dp1[i] = max(dp1[i - 1] + a[i], a[i]) max_val = max(max_val, dp1[i]) dp2 = [0] * (n + 1) for i in range(1, n + 1): dp2[i] = max(dp1[i - 1] + a[i] * x, dp2[i - 1] + a[i] * x, a[i] * x) max_val = max(max_val, dp2[i]) dp3 = [0] * (n + 1) for i in range(1, n + 1): dp3[i] = max(dp2[i - 1] + a[i], dp3[i - 1] + a[i], a[i]) max_val = max(max_val, dp3[i]) print(max_val) solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
import sys def main(): print = out.append n, x = get_list() li = get_list() ans = best1 = best2 = best3 = 0 for a in li: best3 = max(0, best1 + a, best2 + a, best3 + a) best2 = max(0, best1 + x * a, best2 + x * a) best1 = max(0, best1 + a) ans = max(ans, best1, best2, best3) print(ans) input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ out = [] get_int = lambda: int(input()) get_list = lambda: list(map(int, input().split())) main() print(*out, sep="\n")
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) a = list(map(int, input().split())) b = [([0] * 3) for i in range(n)] for i in range(n): if i == 0: b[i][0] = max(a[i], 0) b[i][1] = a[i] * x b[i][2] = max(b[i][1], a[i]) else: b[i][0] = max(b[i - 1][0] + a[i], 0) b[i][1] = max(b[i - 1][1] + a[i] * x, b[i - 1][0] + a[i] * x, 0) b[i][2] = max(b[i][1], b[i - 1][2] + a[i]) print(max(max(b, key=lambda x: max(x))))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def main(): n, x = list(map(int, input().split())) l = list(map(int, input().split())) ans = a = b = c = 0 for i in l: c = max(0, i, a + i, b + i, c + i) b = max(0, i * x, a + i * x, b + i * x) a = max(0, i, a + i) ans = max(0, ans, a, b, c) print(ans) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) a = list(map(int, input().split())) dp = [[(0) for i in range(3)] for j in range(n + 1)] a = [0] + a res = 0 for i in range(1, n + 1): dp[i][0] = max(0, dp[i - 1][0] + a[i], a[i]) dp[i][1] = max(0, dp[i - 1][0] + a[i] * x, dp[i - 1][1] + a[i] * x, a[i] * x) dp[i][2] = max( 0, a[i], dp[i - 1][0] + a[i], dp[i - 1][1] + a[i], dp[i - 1][2] + a[i] ) res = max(res, dp[i][0], dp[i][1], dp[i][2]) print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
from sys import stdin, stdout nmbr = lambda: int(input()) lst = lambda: list(map(int, input().split())) def fn(pos, state): if pos < 0: return 0 if state == 2: ans = max(a[pos], x * a[pos] + fn(pos - 1, 1), a[pos] + fn(pos - 1, 2)) elif state == 1: ans = max(a[pos], x * a[pos] + fn(pos - 1, 1), a[pos] + fn(pos - 1, 0)) else: ans = max(a[pos], a[pos] + fn(pos - 1, 0)) return max(0, ans) for _ in range(1): n, x = lst() a = lst() dp = [[(0) for i in range(3)] for i in range(1 + n)] for i in range(1, n + 1): for j in range(3): if j == 0: dp[i][j] = max(0, a[i - 1], a[i - 1] + dp[i - 1][0]) elif j == 1: dp[i][j] = max( 0, a[i - 1], x * a[i - 1] + dp[i - 1][1], a[i - 1] + dp[i - 1][0] ) else: dp[i][j] = max(0, x * a[i - 1] + dp[i - 1][1], a[i - 1] + dp[i - 1][2]) ans = 0 for i in range(1, 1 + n): ans = max(ans, dp[i][2]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def solve(): N, X = map(int, input().split()) A = [int(k) for k in input().split()] ans = 0 cur_max1 = 0 cur_max2 = 0 cur_max3 = 0 for a in A: cur_max1 = max(a, cur_max1 + a) cur_max2 = max(a * X, a * X + cur_max2, cur_max1) cur_max3 = max(a, cur_max3 + a, cur_max2) ans = max(ans, cur_max1, cur_max2, cur_max3, 0) print(ans) solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) cur1 = cur2 = cur = res = 0 for a in map(int, input().split()): cur1 = max(cur1 + a, 0) cur2 = max(cur2 + a * x, cur1) cur = max(cur + a, cur2) res = max(res, cur) print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, m = map(int, input().split()) dp = [[([-9487948794879487] * 3) for j in range(3)] for i in range(n + 1)] nums = list(map(int, input().split())) dp[0][0][0] = 0 dp[0][1][0] = 0 for i in range(1, n + 1): dp[i][0][0] = dp[i - 1][0][0] dp[i][1][0] = max(dp[i - 1][1][0] + nums[i - 1], dp[i - 1][0][0] + nums[i - 1]) dp[i][2][0] = max(dp[i - 1][2][0], dp[i][1][0]) dp[i][1][1] = max( dp[i - 1][1][0] + nums[i - 1] * m, dp[i - 1][1][1] + nums[i - 1] * m, nums[i - 1] * m, ) dp[i][1][2] = max( dp[i - 1][1][1] + nums[i - 1], dp[i][1][1], dp[i - 1][1][2] + nums[i - 1] ) dp[i][2][2] = max(dp[i][2][0], dp[i][1][2], dp[i - 1][2][2], dp[i][0][0]) print(dp[n][2][2])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = list(map(int, input().split())) a = list(map(int, input().split())) a_mult_x = [0] * n for i in range(n): a_mult_x[i] = a[i] * x dp = [] dp.append([0] * n) dp.append([0] * n) dp.append([0] * n) if a[0] > 0: dp[0][0] = a[0] dp[2][0] = a[0] if a_mult_x[0] > 0: dp[1][0] = a_mult_x[0] for i in range(1, n): dp[0][i] = max(0, dp[0][i - 1]) + a[i] dp[1][i] = max(0, dp[0][i - 1], dp[1][i - 1]) + a_mult_x[i] dp[2][i] = max(0, dp[1][i - 1], dp[2][i - 1]) + a[i] print(max(max(dp[0]), max(dp[1]), max(dp[2])))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def solve(N, X, A): ans = best1 = best2 = best3 = 0 for a in A: best3 = max(0, a, best1 + a, best2 + a, best3 + a) best2 = max(0, X * a, best1 + X * a, best2 + X * a) best1 = max(0, a, best1 + a) ans = max(ans, best1, best2, best3) return ans N, X = [int(x) for x in input().split()] A = [int(x) for x in input().split()] print(solve(N, X, A))
FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, m = input().split(" ") n = int(n) m = int(m) arr = input().split(" ") arr = [0] + [int(x) for x in arr] dp = [([0] * (len(arr) + 1)) for i in range(3)] ans = 0 for i in range(1, len(arr)): dp[0][i] = max(0, dp[0][i - 1]) + arr[i] dp[1][i] = max(dp[0][i - 1], max(0, dp[1][i - 1])) + arr[i] * m dp[2][i] = max(max(dp[0][i - 1], dp[1][i - 1]), max(0, dp[2][i - 1])) + arr[i] ans = max(max(max(dp[0][i], dp[1][i]), dp[2][i]), ans) print(ans)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def go(): n, x = map(int, input().split(" ")) a = [int(i) for i in input().split(" ")] cur1 = cur2 = cur = maximum = 0 for i in range(len(a)): cur1 = max(0, cur1 + a[i]) cur2 = max(cur1, cur2 + x * a[i]) cur = max(cur + a[i], cur2) maximum = max(maximum, cur) return maximum print(go())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def beauty(arr, x): d = [ [[(-float("inf")) for k in range(3)] for j in range(3)] for i in range(len(arr) + 1) ] d[0][0][0] = 0 ans = 0 for i in range(len(arr)): for j in range(3): for k in range(3): if k < 2: d[i][j][k + 1] = max(d[i][j][k + 1], d[i][j][k]) if j < 2: d[i][j + 1][k] = max(d[i][j + 1][k], d[i][j][k]) d[i + 1][j][k] = max( d[i + 1][j][k], d[i][j][k] + [0, arr[i]][j == 1] * [1, x][k == 1] ) ans = max(ans, d[i + 1][j][k]) return ans a, b = map(int, input().strip().split()) lst = list(map(int, input().strip().split())) print(beauty(lst, b))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP LIST NUMBER VAR VAR VAR NUMBER LIST NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) a = list(map(int, input().split())) dp = [[(0) for _ in range(n + 1)] for _ in range(3)] ans = 0 for i in range(n): dp[0][i + 1] = max(0, dp[0][i] + a[i]) dp[1][i + 1] = max(dp[0][i], dp[1][i]) + a[i] * x dp[2][i + 1] = max(dp[0][i], dp[1][i], dp[2][i]) + a[i] ans = max(ans, dp[0][i + 1], dp[1][i + 1], dp[2][i + 1]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) a = [int(i) for i in input().split()] a.insert(0, 0) pref = [0] dp = [[0, 0, 0] for _ in range(n + 1)] for i in range(1, n + 1): dp[i][0] = max(0, dp[i - 1][0] + a[i]) dp[i][1] = max(0, dp[i - 1][0] + x * a[i], dp[i - 1][1] + x * a[i]) dp[i][2] = max(0, dp[i][0], dp[i][1], dp[i - 1][2] + a[i]) maxa = 0 for i in range(len(dp)): maxa = max(maxa, dp[i][2]) print(maxa)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
def main(): n, x = map(int, input().split()) a = list(map(int, input().split())) dp = [[0, 0, 0] for _ in range(n)] dp[0][0] = max(0, a[0]) dp[0][1] = max(0, x * a[0]) answer = max(dp[0]) for i in range(1, n): dp[i][0] = max(dp[i - 1][0] + a[i], a[i]) dp[i][1] = max(dp[i - 1][1] + x * a[i], x * a[i], dp[i - 1][0] + x * a[i]) dp[i][2] = max(dp[i - 1][2] + a[i], dp[i - 1][1] + a[i]) answer = max(answer, *dp[i]) print(answer) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR 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 ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
IN = input().split() n = int(IN[0]) x = int(IN[1]) A = input().split() dp = [0] * 3 for i in range(0, 3): dp[i] = [0] * (n + 1) ans = 0 for i in range(0, n): idx = i + 1 cur = int(A[i]) dp[0][idx] = max(0, dp[0][idx - 1] + cur) dp[1][idx] = max(0, max(dp[0][idx - 1], dp[1][idx - 1]) + cur * x) dp[2][idx] = max(0, max(dp[2][idx - 1], dp[1][idx - 1]) + cur) for j in range(0, 3): ans = max(ans, dp[j][idx]) print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
N, X = list(map(int, input().split())) a_list = list(map(int, input().split())) dp = [([0] * 5) for _ in range(303030)] for i in range(N): a = a_list[i] dp[i + 1][0] = 0 dp[i + 1][1] = max(dp[i][1] + a, dp[i + 1][0]) dp[i + 1][2] = max(dp[i][2] + a * X, dp[i + 1][1]) dp[i + 1][3] = max(dp[i][3] + a, dp[i + 1][2]) dp[i + 1][4] = max(dp[i][4], dp[i + 1][3]) print(dp[N][4])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, m = map(int, input().split()) a = list(map(int, input().split())) def factiry(arr, mul): curMax, mulMax, gloMax, cur = 0, 0, 0, 0 for i in range(n): curMax = max(arr[i] + curMax, 0) mulMax = max(mulMax + arr[i] * mul, curMax) cur = max(cur + arr[i], mulMax) gloMax = max(gloMax, cur) return gloMax total = factiry(a, m) print(total)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, k = map(int, input().split()) l = [*map(int, input().split())] dp = [([0] * 3) for i in range(n)] for i in range(n): for j in range(3): dp[i][j] = float("inf") ans = 0 for i in range(n): if i == 0: dp[i][0] = max(l[i], 0) dp[i][1] = max(l[i] * k, 0) dp[i][2] = max(l[i], 0) else: dp[i][0] = max(dp[i - 1][0] + l[i], 0) dp[i][1] = max(max(dp[i - 1][0], dp[i - 1][1]) + l[i] * k, 0) dp[i][2] = max(max(dp[i - 1][1], dp[i - 1][2]) + l[i], 0) ans = max(ans, max(dp[i][0], dp[i][1], dp[i][2])) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = [int(x) for x in input().split()] data = [int(x) for x in input().split()] dp = [[(0) for _ in range(3)] for _ in range(n + 1)] for i, v in enumerate(data): i += 1 dp[i][0] = max(dp[i - 1][0] + v, v) dp[i][1] = max(dp[i - 1][0] + v * x, v * x, dp[i - 1][1] + v * x) dp[i][2] = max(dp[i - 1][1] + v, dp[i - 1][2] + v) print(max([x for d in dp for x in d]))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = list(map(int, input().split())) l = list(map(int, input().split())) b = [0] * n f = [0] * n pref = [0] * n pref[0] = l[0] for i in range(1, n): pref[i] = pref[i - 1] + l[i] b[0] = x * l[0] mini = 0 for i in range(1, n): mini = min(mini, pref[i - 1]) b[i] = x * l[i] + max(b[i - 1], pref[i - 1] - mini) f[n - 1] = l[n - 1] * x maksi = pref[n - 1] for i in range(1, n): j = n - i - 1 maksi = max(maksi, pref[j]) f[j] = x * l[j] + max(f[j + 1], maksi - pref[j]) wyn = -100000000000000000000000 for i in range(n): wyn = max(wyn, f[i] + b[i] - x * l[i]) mini = 0 wyn1 = -100000000000000000000000 for i in range(n): mini = min(mini, pref[i]) wyn1 = max(wyn1, pref[i] - mini) print(max(wyn, wyn1))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR 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 FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = [int(i) for i in input().split()] A = [int(i) for i in input().split()] dp = [[(-(10**18)) for i in range(5)] for j in range(len(A))] for i in range(n - 1, -1, -1): if 1: nxt = [0, 0, 0, 0, 0] if i != n - 1: nxt = dp[i + 1] coeff = [0, 1, x, 1, 0] for j in range(5): for xx in range(j, len(coeff)): dp[i][j] = max(dp[i][j], coeff[xx] * A[i] + nxt[xx]) print(max(dp[0]))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
n, x = map(int, input().split()) v = list(map(int, input().split())) def DP_Beautiful_array(v, n, x): dp = [([0] * 3) for i in range(n)] dp[0][0] = v[0] dp[0][1] = x * v[0] answer = max(0, max(v[0], x * v[0])) for i in range(1, n, 1): dp[i][0] = max(v[i], v[i] + dp[i - 1][0]) dp[i][1] = max(x * v[i], x * v[i] + max(dp[i - 1][1], dp[i - 1][0])) dp[i][2] = max(v[i], v[i] + max(dp[i - 1][1], dp[i - 1][2])) answer = max(answer, max(dp[i][0], max(dp[i][1], dp[i][2]))) print(answer) DP_Beautiful_array(v, n, x)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
import sys def input(): return sys.stdin.readline().rstrip() DXY = [(0, -1), (1, 0), (0, 1), (-1, 0)] INF = 1 << 64 def main(): n, x = map(int, input().split()) a = list(map(int, input().split())) A = [0] + a.copy() revA = [0] + a.copy()[::-1] b = [-INF] * n revb = [-INF] * n ans = 0 for i in range(1, n + 1): A[i] += A[i - 1] revA[i] += revA[i - 1] for i in range(1, n + 1): ans = max(A[i] - A[i - 1], ans) b[i - 1] = max(A[i] - A[i - 1], 0) revb[i - 1] = max(revA[i] - revA[i - 1], 0) A[i] = min(A[i], A[i - 1]) revA[i] = min(revA[i], revA[i - 1]) dp0 = [-INF] * n for i in range(n): if i > 0: dp0[i] = max(dp0[i - 1] + x * a[i], b[i - 1] + x * a[i]) else: dp0[i] = x * a[i] dp1 = [-INF] * n for i in range(n): if i > 0: dp1[i] = max(dp1[i - 1] + x * a[n - 1 - i], revb[i - 1] + x * a[n - 1 - i]) else: dp1[i] = x * a[n - 1 - i] for i in range(n): ans = max(ans, dp0[i] + dp1[n - 1 - i] - x * a[i]) print(ans) return 0 main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
import sys input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ out = [] get_int = lambda: int(input()) get_list = lambda: list(map(int, input().split())) def main(): n, x = get_list() li = get_list() u_before = [0] * n not_u = [0] * n being_u = [0] * n u_before[0], not_u[0], being_u[0] = li[0], li[0], li[0] * x for i in range(1, n): u_before[i] = max(li[i], li[i] + u_before[i - 1], li[i] + being_u[i - 1]) not_u[i] = max(li[i], li[i] + not_u[i - 1]) being_u[i] = max( li[i] * x, li[i] * x + not_u[i - 1], li[i] * x + being_u[i - 1] ) print(max(max(u_before), max(not_u), max(being_u), 0)) main() print(*out, sep="\n")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) β€” the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) β€” the array $a$. -----Output----- Print one integer β€” the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
import sys input = sys.stdin.readline n, x = map(int, input().split()) A = list(map(int, input().split())) DP0 = [0] * (n + 1) DP1 = [0] * (n + 1) DP2 = [0] * (n + 1) for i in range(n): DP0[i] = max(DP0[i - 1] + A[i], A[i], 0) DP1[i] = max(DP0[i - 1] + A[i] * x, DP1[i - 1] + A[i] * x, DP0[i]) DP2[i] = max(DP2[i - 1] + A[i], DP1[i - 1] + A[i], DP1[i]) print(max(DP2))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
n = int(input()) INF = -1 arr = [0] * n d = [0] * 201 l = list(map(int, input().split())) e = list(map(int, input().split())) s = 0 for i in range(n): arr[i] = l[i], e[i] s += e[i] d[e[i]] += 1 l = [] e = [] ans = [INF] * 3 ans[2] = 10**10 p = arr[-1][0] k = [0, 0] arr.sort() arr = [(-1, -1)] + arr e = 0 c = 0 for i in range(n, -1, -1): if arr[i][0] == p: c += 1 e += arr[i][1] d[arr[i][1]] -= 1 else: if c == 1: ans[0] = max(ans[0], e) elif c == 2: ans[1] = max(ans[1], arr[i + 1][1] + arr[i + 2][1]) if c >= 2: mx = c + c - 1 eng = k[1] if k[0] >= n - mx: ans[2] = min(ans[2], eng) else: g = n - mx - k[0] j = 1 while j < 201 and g > 0: if d[j] > 0: if g > d[j]: eng += d[j] * j g -= d[j] else: eng += g * j g = 0 j += 1 if g == 0: ans[2] = min(ans[2], eng) k[0] += c k[1] += e c = 1 e = arr[i][1] d[arr[i][1]] -= 1 p = arr[i][0] mn = 10**10 for i in range(2): ans[i] = s - ans[i] for i in range(3): if ans[i] != -1 and mn > ans[i]: mn = ans[i] if mn != 10**10: print(mn) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER 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 NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
f = lambda: map(int, input().split()) k, n = 1, f() s = sorted(zip(f(), f()), key=lambda q: q[0]) t = [0] * 201 for l, d in s: t[d] += 1 j = [i for i in range(201) if t[i]] j.reverse() S = sum(i * t[i] for i in j) L, D = s.pop() t[D] -= 1 s.reverse() s.append((0, 0)) m = 0 for l, d in s: if l < L: L = l for i in j: if t[i] > k - 2: D += i * (k - 1) break D += i * t[i] k -= t[i] m = max(m, D) k = D = 0 k += 1 D += d t[d] -= 1 if not t[d]: j.remove(d) print(S - m)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
f = lambda: list(map(int, input().split())) n, p, m, s = input(), {}, 0, sorted(zip(f(), f()), key=lambda q: -q[1]) for L, d in s: k, D = p.get(L, (-1, 0)) p[L] = k + 1, D + d for L, (k, D) in list(p.items()): if k: for l, d in s: if l < L: D += d k -= 1 if k == 0: break m = max(D, m) print(sum(d for l, d in s) - m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR DICT NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
def read_data(): n = int(input()) Ls = list(map(int, input().split())) Ds = list(map(int, input().split())) return n, Ls, Ds def solve(n, Ls, Ds): freqD = [0] * 201 for d in Ds: freqD[d] += 1 LDs = list(zip(Ls, Ds)) LDs.sort(reverse=True) prevL = 0 ni = 0 record = float("inf") cost_long = 0 cost_me = 0 for L, D in LDs: if prevL != L: n -= ni record = min(record, cost_long + calc_cost(ni, n, freqD)) prevL = L ni = 0 cost_long += cost_me if cost_long >= record: return record cost_me = 0 freqD[D] -= 1 cost_me += D ni += 1 record = min(record, cost_long) return record def calc_cost(ni, n, freqD): if n < ni: return 0 cost = 0 for d, f in enumerate(freqD): if n - f >= ni: cost += f * d n -= f else: cost += (n - ni + 1) * d return cost return float("inf") n, Ls, Ds = read_data() print(solve(n, Ls, Ds))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
def main(): _, dd, res = input(), {}, 0 he = list(zip(list(map(int, input().split())), list(map(int, input().split())))) for h, e in he: le, x = dd.get(h, (-1, 0)) dd[h] = le + 1, x + e he.sort(key=lambda _: _[1], reverse=True) for h, (le, x) in list(dd.items()): if le: for h1, e in he: if h1 < h: x += e le -= 1 if not le: break if res < x: res = x print(sum(e for h, e in he) - res) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR DICT NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
n = int(input()) lengths = list(map(int, input().split())) costs = list(map(int, input().split())) sum = 0 length_to_sum = {} length_to_count = {} cost_to_lengths = {} for i in range(n): length, cost = lengths[i], costs[i] sum += cost length_to_sum[length] = length_to_sum.setdefault(length, 0) + cost length_to_count[length] = length_to_count.setdefault(length, 0) + 1 cost_to_lengths.setdefault(cost, []).append(length) length_set = set(lengths) for lengths in cost_to_lengths.values(): lengths.sort() unique_costs = list(reversed(sorted(cost_to_lengths.keys()))) best = -1 for length in length_set: total = sum - length_to_sum[length] seek = length_to_count[length] - 1 if seek != 0: for cost in unique_costs: for x in cost_to_lengths[cost]: if x >= length: break total -= cost seek -= 1 if seek == 0: break if seek == 0: break if best == -1 or total < best: best = total print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL FUNC_CALL VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
from sys import stdin, stdout def main(): n = int(stdin.readline()) leg = [int(_) for _ in stdin.readline().split()] d = [int(_) for _ in stdin.readline().split()] pairedLD = [] for x, y in zip(leg, d): pairedLD.append((x, y)) pairedLD = sorted(pairedLD) legSet = set() suffixSum = dict() legFreq = dict() for length, energy in zip(leg, d): legSet.add(length) if length in suffixSum: suffixSum[length] += energy legFreq[length] += 1 else: suffixSum[length] = energy legFreq[length] = 1 legList = [x for x in legSet] legList = sorted(legList, reverse=True) total = 0 for length in legList: suffixSum[length] += total total = suffixSum[length] ans = int(20000000.0) toRemove = 0 available = 0 removable = [0] * 201 legList = sorted(legList) listLen = len(legList) idx = 0 for i in range(listLen): curr = 0 if i + 1 < listLen: curr += suffixSum[legList[i + 1]] toRemove = available - (legFreq[legList[i]] - 1) if toRemove < 0: toRemove = 0 for j in range(1, 201): if removable[j] != 0 and removable[j] <= toRemove: curr += removable[j] * j toRemove -= removable[j] elif removable[j] > toRemove: curr += toRemove * j toRemove = 0 if toRemove == 0: break available += legFreq[legList[i]] for j in range(legFreq[legList[i]]): removable[pairedLD[idx][1]] += 1 idx += 1 ans = min(ans, curr) print(ans) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
def buscaEnergia(perna, qtd): total = 0 if qtd == 0: return total for i in range(len(d) - 1, -1, -1): for v in d[i]: if v < perna: qtd -= 1 total += i else: break if qtd == 0: break if qtd == 0: break return total n = int(input()) tamanhoPerna = [int(i) for i in input().split()] energiaPerna = [int(i) for i in input().split()] d = [[] for i in range(201)] cntPerna = [0] * 100001 sumPerna = [0] * 100001 corteTotal = 0 maxTam = 0 for i in range(len(tamanhoPerna)): d[energiaPerna[i]].append(tamanhoPerna[i]) sumPerna[tamanhoPerna[i]] += energiaPerna[i] cntPerna[tamanhoPerna[i]] += 1 corteTotal += energiaPerna[i] maxTam = max(tamanhoPerna[i], maxTam) for i in range(len(d)): d[i].sort() currentMin = float("inf") for perna in tamanhoPerna: somaMesa = sumPerna[perna] somaMesa += buscaEnergia(perna, cntPerna[perna] - 1) currentMin = min(currentMin, corteTotal - somaMesa) print(currentMin)
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
n = int(input()) legs = list(zip(list(map(int, input().split())), list(map(int, input().split())))) legs.sort(key=lambda x: x[1], reverse=True) cnt = {} s = 0 for i in range(n): s += legs[i][1] if legs[i][0] not in cnt: cnt[legs[i][0]] = [1, legs[i][1]] else: cnt[legs[i][0]][0] += 1 cnt[legs[i][0]][1] += legs[i][1] temp = sorted(cnt.items()) mn = 9999999999999 f = 0 while temp: l, t = temp.pop() c, e = t s -= e val = s i = 0 count = 0 while count < c - 1: if legs[i][0] < l: count += 1 val -= legs[i][1] i += 1 if i == n: break if val + f < mn: mn = val + f f += e print(mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER LIST NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
def increment(d, key, energy): if key in d.keys(): d[key][0] += 1 d[key][1] += energy else: d[key] = [1, energy] legs = int(input()) lengths = list(map(int, input().split())) energies = list(map(int, input().split())) pairsEL = [] for x in range(legs): pairsEL.append([energies[x], lengths[x]]) pairsEL.sort() pairsEL.reverse() lengthMap = {} for x in range(legs): increment(lengthMap, lengths[x], energies[x]) totalEnergy = sum(energies) maxEnergy = 0 for key in reversed(sorted(lengthMap.keys())): currEnergy = lengthMap[key][1] legsToAdd = lengthMap[key][0] - 1 for pair in pairsEL: if pair[1] < key: if legsToAdd > 0: currEnergy += pair[0] legsToAdd -= 1 else: break maxEnergy = max(maxEnergy, currEnergy) print(totalEnergy - maxEnergy)
FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) arr = list(zip(a, b)) d, res = {}, 0 for i, j in arr: f, x = d.get(i, (-1, 0)) d[i] = f + 1, x + j arr.sort(key=lambda x: x[1], reverse=True) for it, (f, x) in d.items(): if not f: res = max(x, res) continue for i, j in arr: if i < it: x += j f -= 1 if not f: break res = max(x, res) print(sum(j for i, j in arr) - res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR DICT NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has n legs, the length of the i-th leg is l_{i}. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}Β β€”Β the amount of energy that he spends to remove the i-th leg. A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 10^5)Β β€”Β the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of n integers l_{i} (1 ≀ l_{i} ≀ 10^5), where l_{i} is equal to the length of the i-th leg of the table. The third line of the input contains a sequence of n integers d_{i} (1 ≀ d_{i} ≀ 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table. -----Output----- Print a single integer β€” the minimum number of energy units that Arthur needs to spend in order to make the table stable. -----Examples----- Input 2 1 5 3 2 Output 2 Input 3 2 4 4 1 1 1 Output 0 Input 6 2 2 1 1 3 3 4 3 5 5 2 1 Output 8
MAX_D = 201 n = int(input()) l = list(map(int, input().split())) d = list(map(int, input().split())) legs = sorted(list(zip(l, d))) d_suffix = [legs[0][1]] for i in range(1, n): d_suffix.append(d_suffix[-1] + legs[i][1]) ans = d_suffix[-1] cnt = {} left = 0 right = 0 while left < n: while right < n and legs[left][0] == legs[right][0]: right += 1 to_remove = left - (right - left - 1) curr_d = d_suffix[-1] - d_suffix[right - 1] for i in range(1, MAX_D): if to_remove <= 0: break used = min(to_remove, cnt.get(i, 0)) to_remove -= used curr_d += used * i ans = min(ans, curr_d) while left < right: cnt[legs[left][1]] = cnt.get(legs[left][1], 0) + 1 left += 1 print(ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings. There are n rings in factory's stock. The i-th ring has inner radius a_{i}, outer radius b_{i} and height h_{i}. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if b_{j} ≀ b_{i}. Rings should not fall one into the the other. That means one can place ring j on the ring i only if b_{j} > a_{i}. The total height of all rings used should be maximum possible. -----Input----- The first line of the input contains a single integer n (1 ≀ n ≀ 100 000)Β β€” the number of rings in factory's stock. The i-th of the next n lines contains three integers a_{i}, b_{i} and h_{i} (1 ≀ a_{i}, b_{i}, h_{i} ≀ 10^9, b_{i} > a_{i})Β β€” inner radius, outer radius and the height of the i-th ring respectively. -----Output----- Print one integerΒ β€” the maximum height of the tower that can be obtained. -----Examples----- Input 3 1 5 1 2 6 2 3 7 3 Output 6 Input 4 1 2 1 1 3 3 4 6 2 5 7 1 Output 4 -----Note----- In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1. In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
import sys n = int(sys.stdin.readline()) valores = [sys.stdin.readline().strip().split(" ") for _ in range(n)] tmp = [0] * n stack = [] valores.sort(key=lambda x: (int(x[1]), int(x[0])), reverse=True) for i in range(n): while len(stack) > 0 and int(valores[stack[-1]][0]) >= int(valores[i][1]): stack.pop() if len(stack) > 0: tmp[i] = tmp[stack[-1]] + int(valores[i][2]) else: tmp[i] += int(valores[i][2]) stack.append(i) sys.stdout.write(str(max(tmp)) + "\n")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings. There are n rings in factory's stock. The i-th ring has inner radius a_{i}, outer radius b_{i} and height h_{i}. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if b_{j} ≀ b_{i}. Rings should not fall one into the the other. That means one can place ring j on the ring i only if b_{j} > a_{i}. The total height of all rings used should be maximum possible. -----Input----- The first line of the input contains a single integer n (1 ≀ n ≀ 100 000)Β β€” the number of rings in factory's stock. The i-th of the next n lines contains three integers a_{i}, b_{i} and h_{i} (1 ≀ a_{i}, b_{i}, h_{i} ≀ 10^9, b_{i} > a_{i})Β β€” inner radius, outer radius and the height of the i-th ring respectively. -----Output----- Print one integerΒ β€” the maximum height of the tower that can be obtained. -----Examples----- Input 3 1 5 1 2 6 2 3 7 3 Output 6 Input 4 1 2 1 1 3 3 4 6 2 5 7 1 Output 4 -----Note----- In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1. In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
from sys import stdin n = int(input()) x, ans, sum = [([0] * 3) for i in range(n)], 0, 0 d = [] for i in range(n): x[i][1], x[i][0], x[i][2] = map(int, stdin.readline().split()) x.sort() for i in range(n - 1, -1, -1): while len(d) > 0 and d[len(d) - 1][1] >= x[i][0]: sum -= d[len(d) - 1][2] d.pop() d.append(x[i]) sum += x[i][2] ans = max(ans, sum) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings. There are n rings in factory's stock. The i-th ring has inner radius a_{i}, outer radius b_{i} and height h_{i}. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if b_{j} ≀ b_{i}. Rings should not fall one into the the other. That means one can place ring j on the ring i only if b_{j} > a_{i}. The total height of all rings used should be maximum possible. -----Input----- The first line of the input contains a single integer n (1 ≀ n ≀ 100 000)Β β€” the number of rings in factory's stock. The i-th of the next n lines contains three integers a_{i}, b_{i} and h_{i} (1 ≀ a_{i}, b_{i}, h_{i} ≀ 10^9, b_{i} > a_{i})Β β€” inner radius, outer radius and the height of the i-th ring respectively. -----Output----- Print one integerΒ β€” the maximum height of the tower that can be obtained. -----Examples----- Input 3 1 5 1 2 6 2 3 7 3 Output 6 Input 4 1 2 1 1 3 3 4 6 2 5 7 1 Output 4 -----Note----- In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1. In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
aux = [(9000000000.0, 0, 0)] n = int(input()) for i in range(n): a, b, h = map(int, input().split()) aux.append((b, a, h)) aux.sort(reverse=True) s, p = [0], [0] * (n + 1) for i in range(1, n + 1): while aux[s[-1]][1] >= aux[i][0]: s.pop() p[i] = p[s[-1]] + aux[i][2] s.append(i) print(max(p))
ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings. There are n rings in factory's stock. The i-th ring has inner radius a_{i}, outer radius b_{i} and height h_{i}. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if b_{j} ≀ b_{i}. Rings should not fall one into the the other. That means one can place ring j on the ring i only if b_{j} > a_{i}. The total height of all rings used should be maximum possible. -----Input----- The first line of the input contains a single integer n (1 ≀ n ≀ 100 000)Β β€” the number of rings in factory's stock. The i-th of the next n lines contains three integers a_{i}, b_{i} and h_{i} (1 ≀ a_{i}, b_{i}, h_{i} ≀ 10^9, b_{i} > a_{i})Β β€” inner radius, outer radius and the height of the i-th ring respectively. -----Output----- Print one integerΒ β€” the maximum height of the tower that can be obtained. -----Examples----- Input 3 1 5 1 2 6 2 3 7 3 Output 6 Input 4 1 2 1 1 3 3 4 6 2 5 7 1 Output 4 -----Note----- In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1. In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
def ri(): return map(int, input().split()) n = int(input()) r = [] T = [] maxt = -1 for i in range(n): r.append(list(ri())) r.sort(key=lambda e: (e[1], e[0])) T.append([r[0][2], r[0][1]]) for i in range(1, n): tmp = r[i][2] l = len(T) for j in range(l): if r[i][0] >= T[-1][1]: T.append([tmp, r[i][1]]) break else: tmp = max(T[-1][0] + r[i][2], tmp) T.pop() else: T.append([tmp, r[i][1]]) print(max(T)[0])
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings. There are n rings in factory's stock. The i-th ring has inner radius a_{i}, outer radius b_{i} and height h_{i}. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if b_{j} ≀ b_{i}. Rings should not fall one into the the other. That means one can place ring j on the ring i only if b_{j} > a_{i}. The total height of all rings used should be maximum possible. -----Input----- The first line of the input contains a single integer n (1 ≀ n ≀ 100 000)Β β€” the number of rings in factory's stock. The i-th of the next n lines contains three integers a_{i}, b_{i} and h_{i} (1 ≀ a_{i}, b_{i}, h_{i} ≀ 10^9, b_{i} > a_{i})Β β€” inner radius, outer radius and the height of the i-th ring respectively. -----Output----- Print one integerΒ β€” the maximum height of the tower that can be obtained. -----Examples----- Input 3 1 5 1 2 6 2 3 7 3 Output 6 Input 4 1 2 1 1 3 3 4 6 2 5 7 1 Output 4 -----Note----- In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1. In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
n = int(input()) d = [] for i in range(n): a, b, h = map(int, input().split()) d.append((b, a, h)) d.sort() ht = d[len(d) - 1][2] ans = ht s = [] s.append(d[len(d) - 1]) for i in range(n - 2, -1, -1): while s and d[i][0] <= s[len(s) - 1][1]: ht -= s.pop()[2] s.append(d[i]) ht += d[i][2] if ans < ht: ans = ht print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a book with $n$ chapters. Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list. Currently you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book. Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter. Determine how many times you will read the book to understand every chapter, or determine that you will never understand every chapter no matter how many times you read the book. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2\cdot10^4$). The first line of each test case contains a single integer $n$ ($1 \le n \le 2\cdot10^5$) β€” number of chapters. Then $n$ lines follow. The $i$-th line begins with an integer $k_i$ ($0 \le k_i \le n-1$) β€” number of chapters required to understand the $i$-th chapter. Then $k_i$ integers $a_{i,1}, a_{i,2}, \dots, a_{i, k_i}$ ($1 \le a_{i, j} \le n, a_{i, j} \ne i, a_{i, j} \ne a_{i, l}$ for $j \ne l$) follow β€” the chapters required to understand the $i$-th chapter. It is guaranteed that the sum of $n$ and sum of $k_i$ over all testcases do not exceed $2\cdot10^5$. -----Output----- For each test case, if the entire book can be understood, print how many times you will read it, otherwise print $-1$. -----Examples----- Input 5 4 1 2 0 2 1 4 1 2 5 1 5 1 1 1 2 1 3 1 4 5 0 0 2 1 2 1 2 2 2 1 4 2 2 3 0 0 2 3 2 5 1 2 1 3 1 4 1 5 0 Output 2 -1 1 2 5 -----Note----- In the first example, we will understand chapters $\{2, 4\}$ in the first reading and chapters $\{1, 3\}$ in the second reading of the book. In the second example, every chapter requires the understanding of some other chapter, so it is impossible to understand the book. In the third example, every chapter requires only chapters that appear earlier in the book, so we can understand everything in one go. In the fourth example, we will understand chapters $\{2, 3, 4\}$ in the first reading and chapter $1$ in the second reading of the book. In the fifth example, we will understand one chapter in every reading from $5$ to $1$.
import sys input = sys.stdin.readline def solve(): n = int(input()) arr = [list(map(int, input().split())) for _ in range(n)] graph = [[] for _ in range(n)] incoming = [0] * n vals = [1] * n for i in range(n): for j in range(1, len(arr[i])): incoming[i] += 1 graph[arr[i][j] - 1].append(i) q = [] ordering = [] for i in range(n): if incoming[i] == 0: q.append(i) while q: node = q.pop() ordering.append(node) for nei in graph[node]: incoming[nei] -= 1 vals[nei] = max(vals[nei], vals[node] + 1 if nei < node else vals[node]) if incoming[nei] == 0: q.append(nei) return -1 if len(ordering) < n else max(vals) for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a book with $n$ chapters. Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list. Currently you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book. Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter. Determine how many times you will read the book to understand every chapter, or determine that you will never understand every chapter no matter how many times you read the book. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2\cdot10^4$). The first line of each test case contains a single integer $n$ ($1 \le n \le 2\cdot10^5$) β€” number of chapters. Then $n$ lines follow. The $i$-th line begins with an integer $k_i$ ($0 \le k_i \le n-1$) β€” number of chapters required to understand the $i$-th chapter. Then $k_i$ integers $a_{i,1}, a_{i,2}, \dots, a_{i, k_i}$ ($1 \le a_{i, j} \le n, a_{i, j} \ne i, a_{i, j} \ne a_{i, l}$ for $j \ne l$) follow β€” the chapters required to understand the $i$-th chapter. It is guaranteed that the sum of $n$ and sum of $k_i$ over all testcases do not exceed $2\cdot10^5$. -----Output----- For each test case, if the entire book can be understood, print how many times you will read it, otherwise print $-1$. -----Examples----- Input 5 4 1 2 0 2 1 4 1 2 5 1 5 1 1 1 2 1 3 1 4 5 0 0 2 1 2 1 2 2 2 1 4 2 2 3 0 0 2 3 2 5 1 2 1 3 1 4 1 5 0 Output 2 -1 1 2 5 -----Note----- In the first example, we will understand chapters $\{2, 4\}$ in the first reading and chapters $\{1, 3\}$ in the second reading of the book. In the second example, every chapter requires the understanding of some other chapter, so it is impossible to understand the book. In the third example, every chapter requires only chapters that appear earlier in the book, so we can understand everything in one go. In the fourth example, we will understand chapters $\{2, 3, 4\}$ in the first reading and chapter $1$ in the second reading of the book. In the fifth example, we will understand one chapter in every reading from $5$ to $1$.
t = int(input()) while t: n = int(input()) ct = [1] * n pre_req = [0] * n dt = {i: [] for i in range(n)} queue = [] for j in range(n): a = list(map(int, input().split())) for i in range(1, a[0] + 1): dt[a[i] - 1].append(j) pre_req[j] = a[0] if a[0] == 0: queue += [j] ind = 0 while ind < len(queue): g = queue[ind] for i in dt[g]: pre_req[i] -= 1 ct[i] = max(ct[i], ct[g] + [0, 1][i < g]) if pre_req[i] == 0: queue.append(i) ind += 1 if max(pre_req) != 0: print(-1) else: print(max(ct)) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR LIST NUMBER NUMBER VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
You are given a book with $n$ chapters. Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list. Currently you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book. Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter. Determine how many times you will read the book to understand every chapter, or determine that you will never understand every chapter no matter how many times you read the book. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2\cdot10^4$). The first line of each test case contains a single integer $n$ ($1 \le n \le 2\cdot10^5$) β€” number of chapters. Then $n$ lines follow. The $i$-th line begins with an integer $k_i$ ($0 \le k_i \le n-1$) β€” number of chapters required to understand the $i$-th chapter. Then $k_i$ integers $a_{i,1}, a_{i,2}, \dots, a_{i, k_i}$ ($1 \le a_{i, j} \le n, a_{i, j} \ne i, a_{i, j} \ne a_{i, l}$ for $j \ne l$) follow β€” the chapters required to understand the $i$-th chapter. It is guaranteed that the sum of $n$ and sum of $k_i$ over all testcases do not exceed $2\cdot10^5$. -----Output----- For each test case, if the entire book can be understood, print how many times you will read it, otherwise print $-1$. -----Examples----- Input 5 4 1 2 0 2 1 4 1 2 5 1 5 1 1 1 2 1 3 1 4 5 0 0 2 1 2 1 2 2 2 1 4 2 2 3 0 0 2 3 2 5 1 2 1 3 1 4 1 5 0 Output 2 -1 1 2 5 -----Note----- In the first example, we will understand chapters $\{2, 4\}$ in the first reading and chapters $\{1, 3\}$ in the second reading of the book. In the second example, every chapter requires the understanding of some other chapter, so it is impossible to understand the book. In the third example, every chapter requires only chapters that appear earlier in the book, so we can understand everything in one go. In the fourth example, we will understand chapters $\{2, 3, 4\}$ in the first reading and chapter $1$ in the second reading of the book. In the fifth example, we will understand one chapter in every reading from $5$ to $1$.
t = int(input()) for i in range(t): n = int(input()) l = [1] * n d = {i: [] for i in range(n)} q = [] m = [0] * n for j in range(n): a = [*map(int, input().split())] for i in range(1, a[0] + 1): d[a[i] - 1].append(j) m[j] = a[0] if a[0] == 0: q += [[a[0], j]] y = 0 while y < len(q): g = q[y] for i in d[g[1]]: m[i] -= 1 l[i] = max(l[i], l[g[1]] + [0, 1][i < g[1]]) if m[i] == 0: q += [[0, i]] y += 1 if max(m) != 0: print(-1) else: print(max(l))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR LIST LIST VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER LIST NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR LIST LIST NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a book with $n$ chapters. Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list. Currently you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book. Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter. Determine how many times you will read the book to understand every chapter, or determine that you will never understand every chapter no matter how many times you read the book. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2\cdot10^4$). The first line of each test case contains a single integer $n$ ($1 \le n \le 2\cdot10^5$) β€” number of chapters. Then $n$ lines follow. The $i$-th line begins with an integer $k_i$ ($0 \le k_i \le n-1$) β€” number of chapters required to understand the $i$-th chapter. Then $k_i$ integers $a_{i,1}, a_{i,2}, \dots, a_{i, k_i}$ ($1 \le a_{i, j} \le n, a_{i, j} \ne i, a_{i, j} \ne a_{i, l}$ for $j \ne l$) follow β€” the chapters required to understand the $i$-th chapter. It is guaranteed that the sum of $n$ and sum of $k_i$ over all testcases do not exceed $2\cdot10^5$. -----Output----- For each test case, if the entire book can be understood, print how many times you will read it, otherwise print $-1$. -----Examples----- Input 5 4 1 2 0 2 1 4 1 2 5 1 5 1 1 1 2 1 3 1 4 5 0 0 2 1 2 1 2 2 2 1 4 2 2 3 0 0 2 3 2 5 1 2 1 3 1 4 1 5 0 Output 2 -1 1 2 5 -----Note----- In the first example, we will understand chapters $\{2, 4\}$ in the first reading and chapters $\{1, 3\}$ in the second reading of the book. In the second example, every chapter requires the understanding of some other chapter, so it is impossible to understand the book. In the third example, every chapter requires only chapters that appear earlier in the book, so we can understand everything in one go. In the fourth example, we will understand chapters $\{2, 3, 4\}$ in the first reading and chapter $1$ in the second reading of the book. In the fifth example, we will understand one chapter in every reading from $5$ to $1$.
def solve(_num=0): n = int(input()) chapters = [] c___deps = [[] for _ in range(n)] for i in range(n): c = [(int(x) - 1) for x in input().split()] c.pop(0) for neigh in c: c___deps[neigh].append(i) chapters.append(c) stack = [] c___rnr = [(0) for _ in range(n)] c___ans = [(-1) for _ in range(n)] for i, c in enumerate(chapters): if c___rnr[i] == len(c): stack.append(i) c___ans[i] = 1 while stack: curr = stack.pop() for dep in c___deps[curr]: c___rnr[dep] += 1 if c___rnr[dep] == len(chapters[dep]): ans = -1 for dd in chapters[dep]: ca = c___ans[dd] if dd > dep: ca += 1 ans = max(ca, ans) c___ans[dep] = ans stack.append(dep) if -1 in c___ans: print(-1) else: print(max(c___ans)) for i in range(int(input())): solve(i)
FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given a book with $n$ chapters. Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list. Currently you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book. Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter. Determine how many times you will read the book to understand every chapter, or determine that you will never understand every chapter no matter how many times you read the book. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2\cdot10^4$). The first line of each test case contains a single integer $n$ ($1 \le n \le 2\cdot10^5$) β€” number of chapters. Then $n$ lines follow. The $i$-th line begins with an integer $k_i$ ($0 \le k_i \le n-1$) β€” number of chapters required to understand the $i$-th chapter. Then $k_i$ integers $a_{i,1}, a_{i,2}, \dots, a_{i, k_i}$ ($1 \le a_{i, j} \le n, a_{i, j} \ne i, a_{i, j} \ne a_{i, l}$ for $j \ne l$) follow β€” the chapters required to understand the $i$-th chapter. It is guaranteed that the sum of $n$ and sum of $k_i$ over all testcases do not exceed $2\cdot10^5$. -----Output----- For each test case, if the entire book can be understood, print how many times you will read it, otherwise print $-1$. -----Examples----- Input 5 4 1 2 0 2 1 4 1 2 5 1 5 1 1 1 2 1 3 1 4 5 0 0 2 1 2 1 2 2 2 1 4 2 2 3 0 0 2 3 2 5 1 2 1 3 1 4 1 5 0 Output 2 -1 1 2 5 -----Note----- In the first example, we will understand chapters $\{2, 4\}$ in the first reading and chapters $\{1, 3\}$ in the second reading of the book. In the second example, every chapter requires the understanding of some other chapter, so it is impossible to understand the book. In the third example, every chapter requires only chapters that appear earlier in the book, so we can understand everything in one go. In the fourth example, we will understand chapters $\{2, 3, 4\}$ in the first reading and chapter $1$ in the second reading of the book. In the fifth example, we will understand one chapter in every reading from $5$ to $1$.
def func2(datalist, n): fished = [0] * (n + 1) sid = 1 eid = 1 xlist = [[]] children = [[] for i in range(n + 1)] xn = 0 for i in range(1, n + 1): if datalist[i][0] == 0: fished[eid] = i eid += 1 xlist.append([1, 0]) xn += 1 else: for id in datalist[i][1:]: children[id].append(i) xlist.append([1, datalist[i][0]]) maxstep = 1 while sid != eid: cid = fished[sid] sid += 1 for id in children[cid]: xlist[id][1] -= 1 if cid < id: xlist[id][0] = max(xlist[id][0], xlist[cid][0]) else: xlist[id][0] = max(xlist[id][0], xlist[cid][0] + 1) if xlist[id][1] == 0: xn += 1 maxstep = max(maxstep, xlist[id][0]) if children[id]: fished[eid] = id eid += 1 if xn != n: return -1 return maxstep t = int(input()) for i in range(t): n = int(input()) datalist = [0] for i in range(n): datalist.append([int(x) for x in input().split()]) print(func2(datalist, n))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER VAR NUMBER FOR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Recently, the bear started studying data structures and faced the following problem. You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: <image>, where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment). Help the bear cope with the problem. Input The first line contains integer n (1 ≀ n ≀ 106). The second line contains n integers x1, x2, ..., xn (2 ≀ xi ≀ 107). The numbers are not necessarily distinct. The third line contains integer m (1 ≀ m ≀ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 ≀ li ≀ ri ≀ 2Β·109) β€” the numbers that characterize the current query. Output Print m integers β€” the answers to the queries on the order the queries appear in the input. Examples Input 6 5 5 7 10 14 15 3 2 11 3 12 4 4 Output 9 7 0 Input 7 2 3 5 7 11 4 8 2 8 10 2 123 Output 0 7 Note Consider the first sample. Overall, the first sample has 3 queries. 1. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. 2. The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7. 3. The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0.
import sys input = sys.stdin.buffer.readline n = int(input()) x = list(map(int, input().split())) mx = max(x) hf = [i for i in range(mx + 1)] p = 2 while p * p <= mx: if hf[p] == p: for m in range(p * p, mx + 1, p): hf[m] = p p += 1 count = [0] * (mx + 1) for e in x: while e > 1: p = hf[e] while e % p == 0: e //= p count[p] += 1 for i in range(mx): count[i + 1] += count[i] m = int(input()) res = [] for _ in range(m): l, r = map(int, input().split()) if l > mx: res.append(0) else: res.append(count[min(r, mx)] - count[l - 1]) print("\n".join(map(str, res)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, A, R, C, K, Q, q_i, q_j): dp = [[(0) for i in range(C)] for i in range(R)] dp[0][0] = A[0][0] for i in range(1, C): dp[0][i] = dp[0][i - 1] + A[0][i] for i in range(1, R): dp[i][0] = dp[i - 1][0] + A[i][0] for i in range(1, R): for j in range(1, C): dp[i][j] = A[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] res = [] for i in range(Q): x, y = q_i[i], q_j[i] d = min(x, y, R - x - 1, C - y - 1) ans = -1 l = 0 r = d while l <= r: mid = l + r >> 1 x1 = x - mid x2 = x + mid y1 = y - mid y2 = y + mid c = dp[x2][y2] if x1 > 0: c -= dp[x1 - 1][y2] if y1 > 0: c -= dp[x2][y1 - 1] if x1 > 0 and y1 > 0: c += dp[x1 - 1][y1 - 1] if c <= K: ans = 2 * mid + 1 l = mid + 1 else: r = mid - 1 if ans == -1: ans = 0 res.append(ans) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, M, R, C, K, Q, q_i, q_j): dp = [[(0) for k in range(C)] for k in range(R)] dp[0][0] = M[0][0] for i in range(1, R): dp[i][0] = dp[i - 1][0] + M[i][0] for j in range(1, C): dp[0][j] = dp[0][j - 1] + M[0][j - 1] for i in range(1, R): for j in range(1, C): dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + M[i][j] result = [] for q in range(Q): i = q_i[q] j = q_j[q] min_dist = min(i, j, R - 1 - i, C - 1 - j) l = 0 u = min_dist ans = 0 while l <= u: mid = (l + u) // 2 x1 = i - mid y1 = j - mid x2 = i + mid y2 = j + mid count = dp[x2][y2] if x1 > 0: count -= dp[x1 - 1][y2] if y1 > 0: count -= dp[x2][y1 - 1] if x1 > 0 and y1 > 0: count += dp[x1 - 1][y1 - 1] if count <= K: ans = 2 * mid + 1 l = mid + 1 else: u = mid - 1 result.append(ans) return result if __name__ == "__main__": t = int(input()) for _ in range(t): R, C = map(int, input().split()) M = [] for i in range(R): temp = list(map(int, input().split())) M.append(temp) K, Q = list(map(int, input().split())) q_i = list(map(int, input().split())) q_j = list(map(int, input().split())) ob = Solution() res = ob.largestSquare(M, R, C, K, Q, q_i, q_j) for i in res: print(i, end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, M, R, C, K, Q, q_i, q_j): dp = [([0] * (C + 1)) for _ in range(R + 1)] for i in range(R): pres = 0 for j in range(C): pres += M[i][j] dp[i + 1][j + 1] = pres + dp[i][j + 1] ans = [] for x, y in zip(q_i, q_j): max_l = 1 temp = M[x][y] <= K x, y = x + 1, y + 1 i = 1 while True: x1, y1 = x - i, y - i x2, y2 = x + i, y + i i += 1 if ( 0 < x1 < R + 1 and 0 < y1 < C + 1 and 0 < x2 < R + 1 and 0 < y2 < C + 1 ): once = ( dp[x2][y2] - dp[x2][y1 - 1] - dp[x1 - 1][y2] + dp[x1 - 1][y1 - 1] ) if once > K: break else: max_l += 2 else: break if max_l == 1 and not temp: max_l = 0 ans.append(max_l) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, M, R, C, K, Q, q_i, q_j): myDP = [([0] * (C + 1)) for i in range(R + 1)] for i in range(R): for j in range(C): myDP[i + 1][j + 1] = ( M[i][j] + myDP[i + 1][j] + myDP[i][j + 1] - myDP[i][j] ) def Sum(a2, b2, a1, b1): return ( myDP[a2 + 1][b2 + 1] - myDP[a2 + 1][b1] - myDP[a1][b2 + 1] + myDP[a1][b1] ) def Query(i, j): ans = 0 k = 0 while True: r1, c1 = i - k, j - k r2, c2 = i + k, j + k if r1 < 0 or r2 >= R or c1 < 0 or c2 >= C: break s = Sum(r2, c2, r1, c1) if s > K: break ans = 2 * k + 1 k += 1 return ans return [Query(i, j) for i, j in zip(q_i, q_j)]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, M, R, C, K, Q, q_i, q_j): dp = [([0] * (C + 1)) for _ in range(R + 1)] for i in range(R): for j in range(C): dp[i + 1][j + 1] = M[i][j] + dp[i + 1][j] + dp[i][j + 1] - dp[i][j] def sumarea(r2, c2, r1, c1): return dp[r2 + 1][c2 + 1] - dp[r2 + 1][c1] - dp[r1][c2 + 1] + dp[r1][c1] def query(i, j): ans = 0 k = 0 while True: r1, c1 = i - k, j - k r2, c2 = i + k, j + k if r1 < 0 or r2 >= R or c1 < 0 or c2 >= C: break s = sumarea(r2, c2, r1, c1) if s > K: break ans = 2 * k + 1 k += 1 return ans return [query(i, j) for i, j in zip(q_i, q_j)] if __name__ == "__main__": t = int(input()) for _ in range(t): R, C = map(int, input().split()) M = [] for i in range(R): temp = list(map(int, input().split())) M.append(temp) K, Q = list(map(int, input().split())) q_i = list(map(int, input().split())) q_j = list(map(int, input().split())) ob = Solution() res = ob.largestSquare(M, R, C, K, Q, q_i, q_j) for i in res: print(i, end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, M, R, C, K, Q, q_i, q_j): row = [] rsum = 0 for x in M[0]: rsum += x row += [rsum] grid = [row + [0]] for i in range(1, R): row = [] rsum = 0 for j, x in enumerate(M[i]): rsum += x row += [grid[i - 1][j] + rsum] grid += [row + [0]] grid += [[0] * (C + 1)] result = [] for ic, jc in zip(q_i, q_j): rmax = min(ic + 1, R - ic, jc + 1, C - jc) for r in range(rmax, 0, -1): inside = grid[ic + r - 1][jc + r - 1] + grid[ic - r][jc - r] inside -= grid[ic - r][jc + r - 1] + grid[ic + r - 1][jc - r] if inside <= K: break else: r = 0 result.append(0 if r < 1 else 2 * r - 1) return result if __name__ == "__main__": t = int(input()) for _ in range(t): R, C = map(int, input().split()) M = [] for i in range(R): temp = list(map(int, input().split())) M.append(temp) K, Q = list(map(int, input().split())) q_i = list(map(int, input().split())) q_j = list(map(int, input().split())) ob = Solution() res = ob.largestSquare(M, R, C, K, Q, q_i, q_j) for i in res: print(i, end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR VAR VAR LIST VAR ASSIGN VAR LIST BIN_OP VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR LIST BIN_OP VAR LIST NUMBER VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, M, R, C, K, Q, q_i, q_j): for i in range(1, R): M[i][0] += M[i - 1][0] for i in range(1, C): M[0][i] += M[0][i - 1] for i in range(1, R): for j in range(1, C): M[i][j] = M[i - 1][j] + M[i][j - 1] - M[i - 1][j - 1] + M[i][j] res = [0] * Q for k in range(Q): i = q_i[k] j = q_j[k] p = 0 while p + i < R and p + j < C and i - p >= 0 and j - p >= 0: a, b, c = 0, 0, 0 if j - p - 1 >= 0: a = M[i + p][j - p - 1] if i - p - 1 >= 0: b = M[i - p - 1][j + p] if j - p - 1 >= 0 and i - p - 1 >= 0: c = M[i - p - 1][j - p - 1] ans = M[i + p][j + p] - a - b + c if ans > K: break else: res[k] = 2 * p + 1 p += 1 return res
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, M, R, C, K, Q, q_i, q_j): l1 = [] l2 = [([0] * (C + 1)) for _ in range(R + 1)] i = 1 while i <= R: j = 1 while j <= C: l2[i][j] = ( M[i - 1][j - 1] + l2[i - 1][j] + l2[i][j - 1] - l2[i - 1][j - 1] ) j += 1 i += 1 q = 0 while q < Q: i = q_i[q] j = q_j[q] d = min(min(i, j), min(R - i - 1, C - j - 1)) ans = 0 l = 0 h = d while l <= h: m = int((l + h) / 2) x1 = i - m y1 = j - m x2 = i + m y2 = j + m count = ( l2[x2 + 1][y2 + 1] - l2[x1][y2 + 1] - l2[x2 + 1][y1] + l2[x1][y1] ) if count <= K: ans = 2 * m + 1 l = m + 1 else: h = m - 1 l1.append(ans) q += 1 return l1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, matrix, R, C, K, Q, q_i, q_j): countDP = [[(0) for x in range(C)] for x in range(R)] countDP[0][0] = matrix[0][0] final = [] for i in range(1, R): countDP[i][0] = countDP[i - 1][0] + matrix[i][0] for j in range(1, C): countDP[0][j] = countDP[0][j - 1] + matrix[0][j] for i in range(1, R): for j in range(1, C): countDP[i][j] = ( matrix[i][j] + countDP[i - 1][j] + countDP[i][j - 1] - countDP[i - 1][j - 1] ) for q in range(0, Q): i = q_i[q] j = q_j[q] min_dist = min(i, j, R - i - 1, C - j - 1) ans = 0 for k in range(0, min_dist + 1): x1 = i - k x2 = i + k y1 = j - k y2 = j + k count = countDP[x2][y2] if x1 > 0: count -= countDP[x1 - 1][y2] if y1 > 0: count -= countDP[x2][y1 - 1] if x1 > 0 and y1 > 0: count += countDP[x1 - 1][y1 - 1] if count > K: break ans = 2 * k + 1 final.append(ans) return final
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, arr, m, n, k, q, q_i, q_j): def get_count(i1, j1, i2, j2): if i1 == 0 and j1 == 0: return count_arr[i2][j2] if i1 == 0: return count_arr[i2][j2] - count_arr[i2][j1 - 1] if j1 == 0: return count_arr[i2][j2] - count_arr[i1 - 1][j2] return ( count_arr[i2][j2] - count_arr[i2][j1 - 1] - count_arr[i1 - 1][j2] + count_arr[i1 - 1][j1 - 1] ) count_arr = [([0] * n) for _ in range(m)] for i in range(m): sum1 = 0 for j in range(n): sum1 += arr[i][j] count_arr[i][j] += sum1 for j in range(n): sum1 = 0 for i in range(1, m): count_arr[i][j] += count_arr[i - 1][j] ans_arr = [] for i, j in zip(q_i, q_j): if not arr[i][j]: ans = 1 elif k != 0: ans = 1 else: ans = 0 i1, j1 = i - 1, j - 1 i2, j2 = i + 1, j + 1 while i1 >= 0 and j1 >= 0 and i2 < m and j2 < n: ones = get_count(i1, j1, i2, j2) if ones > k: break ans = max(ans, i2 - i1 + 1) i1 -= 1 j1 -= 1 i2 += 1 j2 += 1 ans_arr.append(ans) return ans_arr
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Prefix: def __init__(self, grid): self.numRows = len(grid) self.numCols = len(grid[0]) self.prefix = [ [(0) for cell in range(self.numCols + 1)] for row in range(self.numRows + 1) ] self.build(grid) def build(self, grid): for row in range(1, self.numRows + 1): for col in range(1, self.numCols + 1): self.prefix[row][col] = ( grid[row - 1][col - 1] + self.prefix[row - 1][col] + self.prefix[row][col - 1] - self.prefix[row - 1][col - 1] ) def calcRectangle(self, startRow, startCol, endRow, endCol): fullRec = self.prefix[endRow][endCol] topRec = self.prefix[startRow - 1][endCol] leftRec = self.prefix[endRow][startCol - 1] commonRec = self.prefix[startRow - 1][startCol - 1] recVal = fullRec - topRec - leftRec + commonRec return recVal def checkRec(self, startx, starty, endx, endy, ln, k): startx -= ln starty -= ln endx += ln endy += ln if startx > 0 and starty > 0 and endx <= self.numRows and endy <= self.numCols: val = self.calcRectangle(startx, starty, endx, endy) return val <= k else: return False class Solution: def largestSquare(self, grid, numRows, numCols, k, q, q_i, q_j): res = [] prefix = Prefix(grid) for i, j in zip(q_i, q_j): startx = endx = i + 1 starty = endy = j + 1 lo, hi = 0, min(i, j, numRows - i, numCols - j) ans = 0 while lo <= hi: mid = (lo + hi) // 2 if prefix.checkRec(startx, starty, endx, endy, mid, k): lo = mid + 1 ans = 2 * mid + 1 else: hi = mid - 1 res.append(ans) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, M, R, C, K, Q, q_i, q_j): query_coordinates = zip(q_i, q_j) largest_sq = [(0) for i in range(Q)] subarr_sum_mat = [[(0) for j in range(C)] for i in range(R)] subarr_sum_mat[0][0] = M[0][0] for i in range(R): pref_sum = 0 for j in range(C): pref_sum += M[i][j] subarr_sum_mat[i][j] = pref_sum for j in range(C): for i in range(1, R): subarr_sum_mat[i][j] += subarr_sum_mat[i - 1][j] query_num = 0 for qi, qj in query_coordinates: if qi in {0, R - 1} or qj in {0, C - 1}: if M[qi][qj] <= K: largest_sq[query_num] = 1 else: max_sq_size = 2 * min(qi, qj, R - qi - 1, C - qj - 1) + 1 sq_size = 1 sq_sum = M[qi][qj] while sq_size <= max_sq_size: top_lft_corner = qi - sq_size // 2, qj - sq_size // 2 top_rt_corner = qi - sq_size // 2, qj + sq_size // 2 bott_lft_corner = qi + sq_size // 2, qj - sq_size // 2 bott_rt_corner = qi + sq_size // 2, qj + sq_size // 2 tot_area = subarr_sum_mat[bott_rt_corner[0]][bott_rt_corner[1]] top_area = ( subarr_sum_mat[top_rt_corner[0] - 1][top_rt_corner[1]] if top_rt_corner[0] != 0 else 0 ) left_area = ( subarr_sum_mat[bott_lft_corner[0]][bott_lft_corner[1] - 1] if bott_lft_corner[1] != 0 else 0 ) corner_area = ( subarr_sum_mat[top_lft_corner[0] - 1][top_lft_corner[1] - 1] if top_lft_corner[0] != 0 and top_lft_corner[1] != 0 else 0 ) sq_sum = tot_area - top_area - left_area + corner_area if sq_sum <= K: largest_sq[query_num] = sq_size else: break sq_size += 2 query_num += 1 return largest_sq
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a binary matrix M with R rows and C columns, where each element of the matrix will be 0 or 1. Find the largest square that can be formed with center (i, j) and contains atmost K 1s. There are Q queries, a single query has two integers denoting the centre (i,j) of the square. Example 1: Input: R = 4, C = 5 M = {{1, 0, 1, 0, 0} {1, 0, 1, 1, 1} {1, 1, 1, 1, 1} {1, 0, 0, 1, 0}} K = 9, Q = 1 q_i[] = {1} q_j[] = {2} Output: 3 Explanation: Maximum length square with center at (1, 2) that can be formed is of 3 length from (0, 1) to (2, 3). Example 2: Input: R = 3, C = 3 M = {{1, 1, 1} {1, 1, 1} {1, 1, 1}} K = 9, Q = 2 q_i[] = {1, 2} q_j[] = {1, 2} Output : 3 1 Your Task: You don't need to read input or print anything. Your task is to complete the function largestSquare() which takes 2 integers R, and C followed by a list of lists M denoting the binary matrix and then three integers i,j, and K as input and returns a list of integers denting the largest Square possible for each query. Expected Time Complexity: O(R*C + Q*log(MIN_DIST)), where MIN_DIST is the minimum distance of the center from the edges of the matrix where MIN_DIST is the minimum distance of the center from the edges of the matrix. Expected Auxiliary Space: O(R*C) Constraints: 1 ≀ R,C ≀ 500 1 ≀ Q ≀ 10^{4} 0 ≀ K ≀ R*C 0 ≀ i < R 0 ≀ j < C
class Solution: def largestSquare(self, M, R, C, K, Q, q_i, q_j): psum = [[(0) for _ in range(C)] for _ in range(R)] psum[0][0] = M[0][0] for i in range(1, C): psum[0][i] = psum[0][i - 1] + M[0][i] for i in range(1, R): psum[i][0] = psum[i - 1][0] + M[i][0] for i in range(1, R): for j in range(1, C): psum[i][j] = ( psum[i - 1][j] + psum[i][j - 1] - psum[i - 1][j - 1] + M[i][j] ) def get_square_sum(i, j, d): nonlocal psum res = psum[i + d][j + d] if i - d - 1 >= 0: res -= psum[i - d - 1][j + d] if j - d - 1 >= 0: res -= psum[i + d][j - d - 1] if i - d - 1 >= 0 and j - d - 1 >= 0: res += psum[i - d - 1][j - d - 1] return res res = [] for ix in range(Q): i, j = q_i[ix], q_j[ix] if M[i][j] > K: res.append(0) continue m_dist = min(i, j, R - i - 1, C - j - 1) l, r = 0, m_dist while l < r: mid = (l + r) // 2 sq_sum = get_square_sum(i, j, mid) if sq_sum > K: r = mid else: l = mid + 1 sq_sum = get_square_sum(i, j, l) if sq_sum > K: l -= 1 res.append(1 + 2 * l) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER VAR RETURN VAR