description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def cumsum(it):
total = 0
for x in it:
total += x
yield total
t = int(input())
for i in range(0, t):
n = int(input())
bricks = list(reversed(list(map(int, input().split(" ")))))
if n < 4:
print(sum(bricks))
total = list(cumsum(bricks))
p = total[0:3]
for i in range(3, n):
p.append(total[i] - min(p[-3:]))
print(p[-1]) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | for _ in range(int(input())):
n = int(input())
a = map(int, input().split())
a = list(a)
a.reverse()
if n <= 3:
print(sum(a))
else:
f = [(0) for i in range(n)]
f[0], f[1], f[2] = a[0], a[0] + a[1], a[0] + a[1] + a[2]
su = f[2]
for i in range(3, n):
su += a[i]
f[i] = su - min([f[i - 1], f[i - 2], f[i - 3]])
print(f[n - 1]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | sums = {}
def summ(i):
total = 0
for k in range(i + 1):
total = total + int(a[k])
return total
def do(n):
if n < 4:
return summ(len(a) - 1)
else:
dp = {
(0): int(a[0]),
(1): int(a[0]) + int(a[1]),
(2): int(a[0]) + int(a[1]) + int(a[2]),
}
k = 3
while k < n:
dp[k] = max(
sums[k - 1] - dp[k - 1] + int(a[k]),
sums[k - 2] - dp[k - 2] + int(a[k]) + int(a[k - 1]),
sums[k - 3] - dp[k - 3] + int(a[k]) + int(a[k - 1]) + int(a[k - 2]),
)
k += 1
return dp[k - 1]
T = int(input())
for x in range(T):
N = int(input())
a = input().split(" ")
a.reverse()
sums = {}
total = 0
i = 0
for b in a:
total = total + int(b)
sums[i] = total
i += 1
print(do(N)) | ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def bricks(a):
length = len(a)
bsum = [0] * length
dp = [0] * length
bsum[length - 1] = a[length - 1]
bsum[length - 2] = a[length - 2] + bsum[length - 1]
bsum[length - 3] = a[length - 3] + bsum[length - 2]
dp[length - 1] = bsum[length - 1]
dp[length - 2] = bsum[length - 2]
dp[length - 3] = bsum[length - 3]
for i in range(length - 4, -1, -1):
bsum[i] = a[i] + bsum[i + 1]
one = a[i] + bsum[i + 1] - dp[i + 1]
two = a[i] + a[i + 1] + bsum[i + 2] - dp[i + 2]
three = a[i] + a[i + 1] + a[i + 2] + bsum[i + 3] - dp[i + 3]
dp[i] = max(one, two, three)
return dp[0]
for i in range(int(input())):
input()
print(bricks([int(x) for x in input().split()])) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | from itertools import accumulate
def solve():
n = int(input())
a = [int(i) for i in input().strip().split()]
a.reverse()
cumsum = list(accumulate(a))
result = cumsum[:3]
for i in range(3, n):
result.append(cumsum[i] - min(result[i - 3 : i]))
return result[-1]
t = int(input())
for _ in range(t):
print(solve()) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | t = int(input())
for i in range(t):
n = int(input())
ar = input().split()
ar = [int(e) for e in ar]
for l in range(n // 2):
temp = ar[n - 1 - l]
ar[n - 1 - l] = ar[l]
ar[l] = temp
b = []
m = []
b.append(ar[0])
m.append(0)
for j in range(1, n):
b.append(ar[j] + b[j - 1])
m.append(0)
m[0] = ar[0]
m[1] = ar[0] + ar[1]
m[2] = ar[0] + ar[1] + ar[2]
m[3] = ar[1] + ar[2] + ar[3]
for j in range(4, n):
a = ar[j] + (b[j - 1] - m[j - 1])
c = ar[j] + ar[j - 1] + (b[j - 2] - m[j - 2])
d = ar[j] + ar[j - 1] + ar[j - 2] + b[j - 3] - m[j - 3]
m[j] = max(a, c, d)
print(m[n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | from itertools import accumulate
def main():
ncases = int(input())
for _ in range(ncases):
length = int(input())
bricks = [int(f) for f in reversed(input().split())]
assert len(bricks) == length
cumsum = [0] + list(accumulate(bricks))
score = [0]
for i in range(1, length + 1):
best = 0
for take in range(1, min(3, i) + 1):
best = max(
best, sum(bricks[i - take : i]) + cumsum[i - take] - score[i - take]
)
score.append(best)
print(score[-1])
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def inv(x):
return abs(x - 1)
t = int(input())
for _ in range(t):
n = int(input())
lis = list(map(int, input().split()))
T = [[[0, 0], [0, 0]] for _ in range(n + 3)]
for i, x in reversed(list(enumerate(lis))):
for j in range(2):
for d in range(1, 4):
if T[i + d][inv(j)][j] + sum(lis[i : i + d]) > T[i][j][j]:
T[i][j][j] = T[i + d][inv(j)][j] + sum(lis[i : i + d])
T[i][j][inv(j)] = T[i + d][inv(j)][inv(j)]
print(T[0][0][0]) | FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def best(Bs):
if len(Bs) < 4:
return sum(Bs)
bb, s = [], 0
for i in range(3):
s += Bs[i]
bb.append(s)
for i in range(3, len(Bs)):
s += Bs[i]
bb.append(s - min(bb[-3:]))
return bb[-1]
T = int(input())
for t in range(T):
N = int(input())
Bs = [int(_) for _ in input().split(" ")]
print(best(Bs[::-1])) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def solution(nums):
score = [(0) for i in range(len(nums))]
pick = [(0) for i in range(len(nums))]
score[-1] = nums[-1]
score[-2] = nums[-2] + score[-1]
score[-3] = nums[-3] + score[-2]
pick[-1] = 1
pick[-2] = 2
pick[-3] = 3
for i in range(len(nums) - 4, -1, -1):
if i + pick[i + 1] + 1 >= len(nums):
pick1 = nums[i]
else:
pick1 = nums[i] + score[i + pick[i + 1] + 1]
if i + 1 + pick[i + 2] + 1 >= len(nums):
pick2 = nums[i] + nums[i + 1]
else:
pick2 = nums[i] + nums[i + 1] + score[i + 1 + pick[i + 2] + 1]
if i + 2 + pick[i + 3] + 1 >= len(nums):
pick3 = nums[i] + nums[i + 1] + nums[i + 2]
else:
pick3 = nums[i] + nums[i + 1] + nums[i + 2] + score[i + 2 + pick[i + 3] + 1]
if pick1 > pick2:
score[i] = pick1
pick[i] = 1
else:
score[i] = pick2
pick[i] = 2
if pick3 > score[i]:
score[i] = pick3
pick[i] = 3
return score[0]
numsOfInput = int(input())
for i in range(numsOfInput):
n = int(input())
nums = list(map(int, input().split()))
print(solution(nums)) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | import sys
T = int(sys.stdin.readline())
for case in range(T):
N = int(sys.stdin.readline())
bs = [int(X) for X in sys.stdin.readline().split()]
bs.reverse()
scores = [(sum(bs[:i]), 0) for i in [1, 2, 3]]
for i in range(3, N):
score_take_1 = bs[i] + scores[i - 1][1], scores[i - 1][0]
score_take_2 = bs[i] + bs[i - 1] + scores[i - 2][1], scores[i - 2][0]
score_take_3 = (
bs[i] + bs[i - 1] + bs[i - 2] + scores[i - 3][1],
scores[i - 3][0],
)
scores.append(max(score_take_1, score_take_2, score_take_3))
print(scores[-1][0]) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | for i in range(int(input())):
input()
li = list(reversed([int(x) for x in input().split()]))
su = [li[0]]
for i in range(1, len(li)):
su.append(su[i - 1] + li[i])
dp = [(0) for i in range(len(li) + 1)]
dp[0] = su[0]
dp[1] = su[1]
dp[2] = su[2]
for k in range(3, len(li)):
dp[k] = max(
li[k] + (su[k - 1] - dp[k - 1]),
li[k] + li[k - 1] + li[k - 2] + (su[k - 3] - dp[k - 3]),
li[k] + li[k - 1] + (su[k - 2] - dp[k - 2]),
)
print(dp[len(li) - 1]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | t = int(input())
for times in range(t):
n = int(input())
numbers = [int(num) for num in input().split()]
numbers.reverse()
sumnums = list(numbers)
for i in range(1, n):
sumnums[i] += sumnums[i - 1]
memo = [-1] * (n + 1)
memo[0] = numbers[0]
memo[1] = numbers[1] + memo[0]
memo[2] = numbers[2] + memo[1]
for i in range(3, n):
memo[i] = max(
memo[i],
sumnums[i - 1] - memo[i - 1] + numbers[i],
sumnums[i - 2] - memo[i - 2] + numbers[i - 1] + numbers[i],
sumnums[i - 3] - memo[i - 3] + numbers[i - 2] + numbers[i - 1] + numbers[i],
)
print(memo[n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | T = int(input().strip())
for t in range(T):
n = int(input().strip())
A = list(map(int, input().strip().split(" ")))
A.reverse()
S = [0]
for t in range(1, n + 1):
S.append(S[t - 1] + A[t - 1])
F = [(0) for t in range(n)]
F[0:3] = S[1:4]
for t in range(3, n):
temp1 = A[t] + S[t] - F[t - 1]
temp2 = A[t - 1] + A[t] + S[t - 1] - F[t - 2]
temp3 = A[t - 2] + A[t - 1] + A[t] + S[t - 2] - F[t - 3]
F[t] = max(temp1, temp2, temp3)
print(F[n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | t = int(input())
def run():
n = int(input())
nums = [0, 0] + list(reversed(list(map(int, input().strip().split(" ")))))
state = [(0, 0)] * (n + 2)
for i in range(2, n + 2):
one = nums[i] + state[i - 1 - state[i - 1][1]][0]
two = nums[i] + nums[i - 1] + state[i - 2 - state[i - 2][1]][0]
three = nums[i] + nums[i - 1] + nums[i - 2] + state[i - 3 - state[i - 3][1]][0]
state[i] = max((one, 1), (two, 2), (three, 3))
print(state[-1][0])
for i in range(t):
run() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def max_play(cards):
N = len(cards)
sum_cards = [0] * N
s = 0
for i in range(N):
s += cards[i]
sum_cards[i] = s
dp = [0] * N
for i in [0, 1, 2]:
dp[i] = sum(cards[0 : i + 1])
for i in range(3, N):
dp[i] = max(
cards[i] + sum_cards[i - 1] - dp[i - 1],
cards[i] + cards[i - 1] + sum_cards[i - 2] - dp[i - 2],
cards[i] + cards[i - 1] + cards[i - 2] + sum_cards[i - 3] - dp[i - 3],
)
return dp[N - 1]
test_cases = int(input())
for t in range(test_cases):
n = int(input())
cards = [0] * n
line = input().split()
line.reverse()
for i in range(n):
cards[i] = int(line[i])
print(max_play(cards)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | class BrickPuzzle:
bricks = []
best = {}
numberOfBricks = 0
maxToTake = 3
def __init__(self, bricks):
assert 0 <= len(bricks) <= 10**5
self.bricks = bricks
self.numberOfBricks = len(bricks)
self.calculate()
def takeBricks(self, index, n):
total = 0
while index >= 0 and n > 0:
total += self.bricks[index]
index -= 1
n -= 1
if index - n >= 0:
newIndex = index - n - self.best[index - n][1]
if newIndex >= 0:
total += self.best[newIndex][0]
return total
def calculate(self):
for brickIndex in range(self.numberOfBricks):
bestTotal = 0
bestChoice = 1
for numberTaken in range(1, self.maxToTake + 1, 1):
amount = self.takeBricks(brickIndex, numberTaken)
if amount > bestTotal:
bestTotal = amount
bestChoice = numberTaken
self.best[brickIndex] = [bestTotal, bestChoice]
def getBestScore(self, brickNumber):
assert 1 <= brickNumber <= self.numberOfBricks
return self.best[brickNumber - 1][0]
t = int(input())
for i in range(t):
numberOfBricks = int(input())
numbersOnBricks = input().strip().split(" ")
bricks = []
for brick in numbersOnBricks:
bricks.append(int(brick))
bricks.reverse()
puzzle = BrickPuzzle(bricks)
print(puzzle.getBestScore(numberOfBricks)) | CLASS_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST VAR VAR FUNC_DEF NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | readinput = lambda x: [int(y) for y in x.split()]
def solve(array):
array = array[::-1]
scores = [(0) for _ in range(len(array))]
total = 0
for i in range(3):
total += array[i]
scores[i] = total
for i in range(3, len(array)):
total += array[i]
min_move = min(scores[i - 1], min(scores[i - 2], scores[i - 3]))
scores[i] = total - min_move
return scores[-1]
for testcase in range(int(input())):
N = readinput(input())
array = readinput(input())
print(solve(array)) | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | t = int(input())
for t in range(0, t):
n = int(input())
arr = input().split()
arr.reverse()
sumarr = []
for i in range(0, n):
arr[i] = int(arr[i])
if i >= 1:
sumarr.append(sumarr[i - 1] + arr[i])
else:
sumarr.append(arr[0])
dp = []
dp.append(sumarr[0])
dp.append(sumarr[1])
dp.append(sumarr[2])
for i in range(3, n):
val = max(
sumarr[i - 1] - dp[i - 1] + arr[i],
sumarr[i - 2] - dp[i - 2] + arr[i - 1] + arr[i],
sumarr[i - 3] - dp[i - 3] + arr[i - 1] + arr[i - 2] + arr[i],
)
dp.append(val)
print(dp[n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
arr.reverse()
summary = [0] * n
cache = [0] * n
summary[0] = arr[0]
for i in range(1, n):
summary[i] = summary[i - 1] + arr[i]
for i in range(3):
cache[i] = summary[i]
for i in range(3, n):
cache[i] = max(
summary[i] - cache[i - 1],
summary[i] - cache[i - 2],
summary[i] - cache[i - 3],
)
print(cache[-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL 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 FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def solve(A, n):
A.reverse()
if n <= 3:
sum = 0
for i in range(n):
sum += A[i]
return sum
dp = list(range(n))
sum = 0
for i in range(3):
sum += A[i]
dp[i] = sum, 0
for i in range(3, n):
first = A[i]
second = int()
if first < A[i] + dp[i - 1][1]:
first = A[i] + dp[i - 1][1]
second = dp[i - 1][0]
if first < A[i - 1] + A[i] + dp[i - 2][1]:
first = A[i - 1] + A[i] + dp[i - 2][1]
second = dp[i - 2][0]
if first < A[i - 2] + A[i - 1] + A[i] + dp[i - 3][1]:
first = A[i - 2] + A[i - 1] + A[i] + dp[i - 3][1]
second = dp[i - 3][0]
dp[i] = first, second
return dp[n - 1][0]
T = int(input())
for t in range(T):
n = int(input())
A = input().split()
for i in range(len(A)):
A[i] = int(A[i])
print(solve(A, n)) | FUNC_DEF EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | __author__ = "deepak Singh Mehta(approach_by agutowski :))"
tests = int(input())
for _ in range(tests):
n = int(input())
suff = best = [0] * (n + 3)
arr = list(map(int, input().split()))
for i in range(n - 1, -1, -1):
suff[i] = suff[i + 1] + arr[i]
for i in range(n - 1, -1, -1):
best[i] = suff[i] - min(best[i + 1 : i + 4])
print(best[0]) | ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER 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 BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def sol(bricks):
if len(bricks) <= 3:
return sum(bricks)
solutions = [[0] * len(bricks), [0] * len(bricks)]
solutions[1][-1] = solutions[0][-1] = sum(bricks[-1:])
solutions[1][-2] = solutions[0][-2] = sum(bricks[-2:])
solutions[1][-3] = solutions[0][-3] = sum(bricks[-3:])
index = len(bricks) - 4
while index >= 0:
solutions[0][index] = max(
sum(bricks[index : index + 3]) - solutions[1][index + 3],
sum(bricks[index : index + 2]) - solutions[1][index + 2],
sum(bricks[index : index + 1]) - solutions[1][index + 1],
)
solutions[1][index] = max(
sum(bricks[index : index + 3]) - solutions[0][index + 3],
sum(bricks[index : index + 2]) - solutions[0][index + 2],
sum(bricks[index : index + 1]) - solutions[0][index + 1],
)
index -= 1
return solutions[0][0]
t = int(input())
for test in range(t):
n = int(input())
bricks = list(map(int, input().split()))
table = {}
print((sum(bricks) + sol(bricks)) // 2) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | import sys
T = int(sys.stdin.readline().strip())
for t in range(T):
sys.stdin.readline()
A = list(map(lambda x: int(x.strip()), sys.stdin.readline().split()))
A.reverse()
B = [0] * len(A)
C = [0] * len(A)
C[0] = A[0]
C[1] = A[0] + A[1]
C[2] = A[0] + A[1] + A[2]
B[0] = A[0]
for i in range(1, len(A)):
B[i] = B[i - 1] + A[i]
for i in range(3, len(A)):
m1 = B[i - 1] - C[i - 1] + A[i]
m2 = B[i - 2] - C[i - 2] + A[i] + A[i - 1]
m3 = B[i - 3] - C[i - 3] + A[i] + A[i - 1] + A[i - 2]
C[i] = max(m1, m2, m3)
print(C[-1]) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def getMax(bricks, N):
if N == 0:
return 0
if N <= 3:
return sum(bricks)
scores = [0]
total = 0
for i in range(1, 4):
total += bricks[-i]
scores.append(total)
for i in range(4, N + 1):
total += bricks[-i]
choice_1 = total - scores[i - 1]
choice_2 = total - scores[i - 2]
choice_3 = total - scores[i - 3]
maxScore = max(choice_1, choice_2, choice_3)
scores.append(maxScore)
print(scores[-1])
T = int(input())
for i in range(T):
N = int(input())
bricks = [int(x) for x in input().split()]
getMax(bricks, N) | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def solve(n, scores):
total = [0] * n
dp = [0] * n
total[-1] = scores[-1]
for i in range(n - 2, -1, -1):
total[i] = total[i + 1] + scores[i]
dp[-3:] = total[-3:]
for i in range(n - 4, -1, -1):
for j in range(1, 4):
dp[i] = max(dp[i], total[i] - dp[i + j])
return dp[0]
def main():
t = int(input())
for _ in range(t):
n = int(input())
scores = list(map(int, input().split()))
print(solve(n, scores))
main() | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | numinputs = int(input())
for dummy in range(numinputs):
n = int(input())
scores = [int(val) for val in input().split()]
dp = [0] * n
sumdp = [0] * n
dp[n - 1] = scores[n - 1]
sumdp[n - 1] = scores[n - 1]
dp[n - 2] = scores[n - 2] + scores[n - 1]
sumdp[n - 2] = scores[n - 2] + scores[n - 1]
dp[n - 3] = scores[n - 3] + scores[n - 2] + scores[n - 1]
sumdp[n - 3] = scores[n - 2] + scores[n - 1] + scores[n - 3]
for i in range(n - 4, -1, -1):
sumdp[i] = sumdp[i + 1] + scores[i]
dp[i] = max(
sumdp[i + 1] - dp[i + 1] + scores[i],
sumdp[i + 2] - dp[i + 2] + scores[i] + scores[i + 1],
sumdp[i + 3] - dp[i + 3] + scores[i] + scores[i + 1] + scores[i + 2],
)
print(dp[0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | __author__ = "Satya"
def play_game():
tc = int(input())
for _ in range(tc):
n = int(input())
numbers = [int(x) for x in input().split()]
if n < 4:
return sum(numbers)
total = [0] * n
total[n - 1] = numbers[n - 1]
for i in range(n - 2, -1, -1):
total[i] = total[i + 1] + numbers[i]
output = [0] * n
output[n - 1] = numbers[n - 1]
output[n - 2] = numbers[n - 2] + output[n - 1]
output[n - 3] = numbers[n - 3] + output[n - 2]
for i in range(n - 4, -1, -1):
a = total[i] - output[i + 1]
b = total[i] - output[i + 2]
c = total[i] - output[i + 3]
if a >= b and a >= c:
output[i] = a
elif b >= a and b >= c:
output[i] = b
else:
output[i] = c
print(output[0])
play_game() | ASSIGN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | import sys
T = int(sys.stdin.readline())
for _ in range(T):
N = int(sys.stdin.readline())
arr = [int(x) for x in sys.stdin.readline().split()]
arr.reverse()
table = [0] * N
table[0] = arr[0], arr[0]
table[1] = sum(arr[:2]), sum(arr[:2])
table[2] = sum(arr[:3]), sum(arr[:3])
s = sum(arr[:3])
for i in range(3, N):
s += arr[i]
best = max(
arr[i] + table[i - 1][1] - table[i - 1][0],
arr[i] + arr[i - 1] + table[i - 2][1] - table[i - 2][0],
arr[i] + arr[i - 1] + arr[i - 2] + table[i - 3][1] - table[i - 3][0],
)
table[i] = best, s
print(table[-1][0]) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | for i in range(int(input())):
t = int(input())
a = [int(e) for e in input().split()]
d = {
(t - 3): a[t - 3] + a[t - 2] + a[t - 1],
(t - 2): a[t - 2] + a[t - 1],
(t - 1): a[t - 1],
t: 0,
}
def max_score(n):
if n in d:
return d[n]
else:
r = max(
a[n] - max_score(n + 1),
a[n] + a[n + 1] - max_score(n + 2),
a[n] + a[n + 1] + a[n + 2] - max_score(n + 3),
)
d[n] = r
return r
for j in reversed(range(t)):
diff = max_score(j)
print((sum(a) + diff) // 2) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def play_game(block_nums, size):
block_nums.reverse()
max_values = [
0,
block_nums[0],
block_nums[0] + block_nums[1],
block_nums[0] + block_nums[1] + block_nums[2],
]
best_picks = [0, 1, 2, 3]
index = 3
for i in range(3, size):
cur_best = -99999999999
cur_num = -9999999999
cur_pick = 0
for j in range(0, 3):
cur_pick += block_nums[i - j]
rival_val = max_values[i - j]
cur_val = cur_pick + max_values[i - j - best_picks[i - j]]
if cur_val - rival_val > cur_best:
max_val = cur_val
cur_best = cur_val - rival_val
cur_num = j + 1
max_values.append(max_val)
best_picks.append(cur_num)
print(max_values[-1])
testcases = int(input())
for i in range(0, testcases):
num = int(input())
array = input().split()
array = [int(x) for x in array]
play_game(array, num) | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def maxScore(B, n):
Mx = [0] * n
Mx[-1] = B[-1]
Mx[-2] = sum(B[-2:])
Mx[-3] = sum(B[-3:])
BackSum = Mx[:]
for i in range(n - 4, -1, -1):
x1 = B[i] + BackSum[i + 1] - Mx[i + 1]
x2 = B[i] + B[i + 1] + BackSum[i + 2] - Mx[i + 2]
x3 = B[i] + B[i + 1] + B[i + 2] + BackSum[i + 3] - Mx[i + 3]
Mx[i] = max(x1, x2, x3)
BackSum[i] = B[i] + BackSum[i + 1]
return Mx[0]
for _ in range(int(input())):
n = int(input())
Bricks = [int(i) for i in input().strip().split()]
if n < 4:
print(sum(Bricks))
else:
print(maxScore(Bricks, n)) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def iterative():
for T in range(int(input())):
n = int(input())
A = [int(x) for x in input().split()]
A.reverse()
Sum = [A[0]]
for i in range(1, n):
Sum.append(Sum[i - 1] + A[i])
dp = [A[0], A[0] + A[1], A[0] + A[1] + A[2]]
for i in range(3, n):
dp.append(
max(
A[i] + (Sum[i - 1] - dp[i - 1]),
A[i] + A[i - 1] + (Sum[i - 2] - dp[i - 2]),
A[i] + A[i - 1] + A[i - 2] + (Sum[i - 3] - dp[i - 3]),
)
)
print(dp[-1])
iterative() | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | T = int(input())
for t in range(T):
n = int(input())
a = list(reversed(list(map(int, input().split()))))
sums = [0] * n
sums[0] = a[0]
for i in range(1, n):
sums[i] = sums[i - 1] + a[i]
dp = [0] * n
dp[0] = a[0]
dp[1] = a[0] + a[1]
dp[2] = a[0] + a[1] + a[2]
for i in range(3, n):
dp[i] = max(
[
a[i] + sums[i - 1] - dp[i - 1],
a[i] + a[i - 1] + sums[i - 2] - dp[i - 2],
a[i] + a[i - 1] + a[i - 2] + sums[i - 3] - dp[i - 3],
]
)
print(dp[n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
l = list(reversed(l))
q = [(0) for i in l]
m = [(0) for i in l]
for i in range(len(l)):
if i <= 2:
if i == 0:
q[i] = l[i]
m[i] = q[i]
else:
q[i] = l[i] + q[i - 1]
m[i] = q[i]
else:
a = l[i] + q[i - 1] - m[i - 1]
b = l[i] + l[i - 1] + q[i - 2] - m[i - 2]
c = l[i] + l[i - 1] + l[i - 2] + q[i - 3] - m[i - 3]
m[i] = max(a, b, c)
q[i] = l[i] + q[i - 1]
print(m[-1]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def f(A):
A = A[::-1]
s = [0] * len(A)
s[0] = A[0]
for i in range(1, len(A)):
s[i] = s[i - 1] + A[i]
dp = [0] * len(A)
dp[0] = A[0]
for i in range(1, min(len(A), 3)):
dp[i] = dp[i - 1] + A[i]
for i in range(3, len(A)):
dp[i] = (
max(
s[i - 1] - dp[i - 1],
s[i - 2] - dp[i - 2] + A[i - 1],
s[i - 3] - dp[i - 3] + A[i - 2] + A[i - 1],
)
+ A[i]
)
return dp[len(A) - 1]
for _ in range(int(input())):
input()
print(f([int(i) for i in input().split()])) | FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def maxScore(n, seq):
prefixSum = [seq[0], seq[0] + seq[1], seq[0] + seq[1] + seq[2]]
table = [seq[0], seq[0] + seq[1], seq[0] + seq[1] + seq[2]]
if n < 4:
return table[-1]
for i in range(3, n):
prefixSum.append(prefixSum[-1] + seq[i])
best = 0
for x in (0, 1, 2):
a = sum(seq[i - x : i + 1])
b = prefixSum[i - x - 1]
c = table[i - x - 1]
best = max(a + b - c, best)
pass
table.append(best)
pass
return table[-1]
t = int(input())
for i in range(t):
n = int(input())
seq = list(map(int, input().split()))
seq.reverse()
print(maxScore(n, seq))
pass | FUNC_DEF ASSIGN VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | test_count = int(input())
for test_id in range(0, test_count):
dummy = input()
values = list(reversed(list(map(int, input().split()))))
total = [0] * len(values)
target = [-1] * len(values)
for i in range(0, len(values)):
best_index = -1
max_value = -1
for j in range(1, 4):
current_total = sum(values[i - j + 1 : i + 1])
if i - j >= 0 and target[i - j] >= 0:
current_total = current_total + total[target[i - j]]
if current_total > max_value:
max_value = current_total
best_index = j
total[i] = max_value
target[i] = i - best_index
print(total[-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def calculate(bricks):
bricks.reverse()
numbricks = len(bricks)
sumx = [bricks[0]] + (numbricks - 1) * [-1]
for x in range(1, numbricks):
sumx[x] = bricks[x] + sumx[x - 1]
dp = [sumx[0], sumx[1], sumx[2]] + (numbricks - 3) * [-1]
for x in range(3, numbricks):
dp[x] = max(
bricks[x] + sumx[x - 1] - dp[x - 1],
bricks[x] + bricks[x - 1] + sumx[x - 2] - dp[x - 2],
bricks[x] + bricks[x - 1] + bricks[x - 2] + sumx[x - 3] - dp[x - 3],
)
return dp[-1]
testcases = int(input())
for x in range(testcases):
numbricks = int(input())
bricks = input().split(" ")
bricks2 = [int(x2) for x2 in bricks]
print(calculate(bricks2)) | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP BIN_OP VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | cache = [(-1) for x in range(0, 10001)]
count = 0
ar = []
def maxSum(x, i):
sum1 = 0
if x == 0:
sum1 = ar[i]
elif x == 1:
sum1 = ar[i] + ar[i + 1]
elif x == 2:
sum1 = ar[i] + ar[i + 1] + ar[i + 2]
elif cache[i] < 0:
totSum = 0
for j in range(i, len(ar)):
totSum += ar[j]
sum1 = max(
totSum - maxSum(x - 1, i + 1),
totSum - maxSum(x - 2, i + 2),
totSum - maxSum(x - 3, i + 3),
)
cache[i] = sum1
else:
sum1 = cache[i]
return sum1
def findSum(N):
sum1 = 0
for i in range(0, N):
sum1 += ar[i]
if i == 0:
cache[i] = ar[i]
elif i == 1:
cache[i] = ar[i] + ar[i - 1]
elif i == 2:
cache[i] = ar[i] + ar[i - 1] + ar[i - 2]
else:
cache[i] = sum1 - min(cache[i - 1], cache[i - 2], cache[i - 3])
return cache[N - 1]
T = int(input())
for j in range(0, T):
N = int(input())
cache = [(-1) for x in range(0, N)]
ar = [int(n) for n in input().split()]
ar.reverse()
print(findSum(N)) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def score(V):
if len(V) <= 3:
return sum(V)
F = [0] * N
F[0] = V[0]
for i in range(1, 3):
F[i] = F[i - 1] + V[i]
for j in range(3, len(V)):
m = 0
c = [0] * 3
for k in range(3):
m += V[j - k]
hi = j - k - 1 if j - k - 1 >= 0 else 0
lo = j - k - 4 if j - k - 4 >= 0 else 0
t = F[lo:hi]
if len(t) < 3:
t.append(0)
o = min(t) if t else 0
c[k] = m + o
F[j] = max(c)
return F[-1]
T = int(input())
for t in range(T):
N = int(input())
V = list(map(int, input().split()))
V.reverse()
print(score(V)) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | cases = int(input())
for _ in range(cases):
size = int(input())
B = [int(x) for x in input().split()]
def score(k, memo={}):
if k in memo:
return memo[k]
res = 0
if size - k <= 3:
res = sum(B[k:])
else:
res = max(
B[k] + min(score(k + 2), score(k + 3), score(k + 4)),
B[k] + B[k + 1] + min(score(k + 3), score(k + 4), score(k + 5)),
B[k]
+ B[k + 1]
+ B[k + 2]
+ min(score(k + 4), score(k + 5), score(k + 6)),
)
memo[k] = res
return res
for k in reversed(range(size)):
score(k)
print(score(0)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF DICT IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | import sys
def read_numbers(line=None):
if line is None:
line = sys.stdin.readline()
return [int(x) for x in line.split()]
def get(l, i, default=0):
try:
return l[i]
except IndexError:
return default
def calculate_best_result(stack, best_results, best_moves, i):
took_one = get(stack, i) + max(
[0] + [get(best_results, i + 1 + move) for move in get(best_moves, i + 1, [])]
)
took_two = (
get(stack, i)
+ get(stack, i + 1)
+ max(
[0]
+ [get(best_results, i + 2 + move) for move in get(best_moves, i + 2, [])]
)
)
took_three = (
get(stack, i)
+ get(stack, i + 1)
+ get(stack, i + 2)
+ max(
[0]
+ [get(best_results, i + 3 + move) for move in get(best_moves, i + 3, [])]
)
)
scores = sorted([(took_one, 1), (took_two, 2), (took_three, 3)], reverse=True)
best_result, best_move = scores[0]
best_moves = set([best_move])
for score, move in scores[1:]:
if score == best_results:
best_moves.add(move)
return best_result, best_moves
def game(stack):
best_results = [0] * len(stack)
best_moves = [set()] * len(stack)
for i in range(len(stack) - 1, -1, -1):
result, move = calculate_best_result(stack, best_results, best_moves, i)
best_results[i] = result
best_moves[i] = move
return best_results[0]
test_cases = read_numbers()[0]
for t in range(test_cases):
n = read_numbers()[0]
stack = read_numbers()
print(game(stack)) | IMPORT FUNC_DEF NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF NUMBER RETURN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER LIST ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER LIST ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER LIST ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR FOR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def main():
t = int(input())
while t:
n = int(input())
a = input().split()
value = [0] * n
for i in range(0, n):
value[i] = int(a[n - 1 - i])
oppo = [0] * n
plyr = [0] * n
plyr[0] = value[0]
plyr[1] = value[0] + value[1]
plyr[2] = value[0] + value[1] + value[2]
cur_total = value[0] + value[1] + value[2]
for i in range(3, n):
plyr[i] = max(
value[i] + oppo[i - 1],
value[i] + value[i - 1] + oppo[i - 2],
value[i] + value[i - 1] + value[i - 2] + oppo[i - 3],
)
cur_total += value[i]
oppo[i] = cur_total - plyr[i]
print(plyr[n - 1])
t -= 1
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | T = int(input())
for i in range(T):
N = int(input())
bricks = list(map(int, input().split()))
bricks.reverse()
sums = [bricks[0]]
for num in bricks[1:]:
sums.append(sums[-1] + num)
if N <= 3:
print(sums[-1])
else:
dp = [sums[0], sums[1], sums[2]]
dp = [bricks[0], bricks[0] + bricks[1], bricks[0] + bricks[1] + bricks[2]]
for i in range(3, len(bricks)):
one = sums[i - 1] - dp[i - 1] + bricks[i]
two = sums[i - 2] - dp[i - 2] + bricks[i] + bricks[i - 1]
three = sums[i - 3] - dp[i - 3] + bricks[i] + bricks[i - 1] + bricks[i - 2]
dp.append(max(one, two, three))
print(dp[-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | T = int(input())
S = []
def buildArrays(A, B, N):
A[0] = S[N - 1]
A[1] = S[N - 1] + S[N - 2]
A[2] = S[N - 1] + S[N - 2] + S[N - 3]
B[0] = 0
B[1] = 0
B[2] = 0
for i in range(3, N):
j = 1
A[i] = S[N - 1 - i] + B[i - 1]
t = S[N - 1 - i] + S[N - 1 - i + 1] + B[i - 2]
if t > A[i]:
A[i] = t
j = 2
t = S[N - 1 - i] + S[N - 1 - i + 1] + S[N - 1 - i + 2] + B[i - 3]
if t > A[i]:
A[i] = t
j = 3
B[i] = A[i - j]
for i in range(T):
N = int(input())
S = list(map(int, input().split()))
A = [(-1) for x in range(N)]
B = [(-1) for x in range(N)]
buildArrays(A, B, N)
print(A[N - 1])
A.clear()
B.clear()
S.clear() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | for _ in range(int(input())):
input()
arr = list(reversed([int(x) for x in input().split()]))
opt = [0, arr[0]]
running_total = arr[0]
if len(arr) > 1:
opt.append(arr[1] + arr[0])
running_total += arr[1]
if len(arr) > 2:
opt.append(arr[2] + arr[1] + arr[0])
running_total += arr[2]
for i in range(3, len(arr)):
running_total += arr[i]
take_one = running_total - opt[-1]
take_two = running_total - opt[-2]
take_three = running_total - opt[-3]
opt.append(max([take_one, take_two, take_three]))
print(opt[-1]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def max_score(bricks):
scores = [(0) for _ in range(len(bricks))]
scores[0] = bricks[0]
scores[1] = scores[0] + bricks[1]
scores[2] = scores[1] + bricks[2]
psum = [(0) for _ in range(len(bricks))]
psum[0] = bricks[0]
for i in range(1, len(psum)):
psum[i] = psum[i - 1] + bricks[i]
for i in range(3, len(bricks)):
scores[i] = max(
(
psum[i - 1] - scores[i - 1] + bricks[i],
psum[i - 2] - scores[i - 2] + bricks[i] + bricks[i - 1],
psum[i - 3] - scores[i - 3] + bricks[i] + bricks[i - 1] + bricks[i - 2],
)
)
return scores[-1]
t = int(input())
for _ in range(t):
input()
bricks = [int(el) for el in input().split()]
print(max_score(list(reversed(bricks)))) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def brick_game(B):
B.reverse()
Sum_B = [B[0]]
for x in B[1:]:
Sum_B.append(Sum_B[-1] + x)
dp = [(0) for _ in range(len(B))]
dp[0], dp[1], dp[2] = Sum_B[0], Sum_B[1], Sum_B[2]
for i in range(3, len(B)):
dp[i] = Sum_B[i] - dp[i - 1]
dp[i] = max(dp[i], Sum_B[i] - dp[i - 2])
dp[i] = max(dp[i], Sum_B[i] - dp[i - 3])
return dp[-1]
T = int(input())
for _ in range(T):
input()
B = list(map(int, input().split()))
print(brick_game(B)) | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR 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 RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR 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 |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | import sys
sys.setrecursionlimit(1000000)
T = int(input())
def max_score1(stack, acc, ptr, dp):
if dp[ptr] == -1:
if ptr < 3:
dp[ptr] = acc[ptr]
else:
opt1 = max_score(stack, acc, ptr - 1, dp)
opt2 = max_score(stack, acc, ptr - 2, dp)
opt3 = max_score(stack, acc, ptr - 3, dp)
dp[ptr] = max(
acc[ptr - 1] - opt1 + stack[ptr],
acc[ptr - 2] - opt2 + stack[ptr] + stack[ptr - 1],
acc[ptr - 3] - opt3 + stack[ptr] + stack[ptr - 1] + stack[ptr - 2],
)
return dp[ptr]
def max_score(stack):
if len(stack) <= 3:
return sum(stack)
n = len(stack)
dp = [(0) for i in range(n)]
acc = stack[:]
for i in range(n - 2, -1, -1):
acc[i] = acc[i] + acc[i + 1]
dp[n - 1], dp[n - 2], dp[n - 3] = acc[n - 1], acc[n - 2], acc[n - 3]
for i in range(4, n + 1):
dp[n - i] = max(
acc[n - i + 1] - dp[n - i + 1] + stack[n - i],
acc[n - i + 2] - dp[n - i + 2] + stack[n - i] + stack[n - i + 1],
acc[n - i + 3]
- dp[n - i + 3]
+ stack[n - i]
+ stack[n - i + 1]
+ stack[n - i + 2],
)
return dp[0]
for i in range(T):
N = int(input())
stack = [int(n) for n in input().split()]
print(max_score(stack)) | IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | testCases = int(input())
for testCase in range(testCases):
n = int(input())
stack = list(map(int, input().split()))
s = [(0) for i in range(n + 1)]
currSum = 0
for i in range(1, n + 1):
currSum += stack[-i]
if i <= 3:
s[i] = currSum
else:
c1 = currSum - s[i - 1]
c2 = currSum - s[i - 2]
c3 = currSum - s[i - 3]
s[i] = max(c1, c2, c3)
print(s[n]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def game_bricks(bricks, n):
states = [[0] * n, [0] * n]
for i in range(n - 1, -1, -1):
states[0][i] = max(
bricks[i] + (states[1][i + 1] if i < n - 1 else 0),
bricks[i]
+ (bricks[i + 1] if i < n - 1 else 0)
+ (states[1][i + 2] if i < n - 2 else 0),
bricks[i]
+ (bricks[i + 1] if i < n - 1 else 0)
+ (bricks[i + 2] if i < n - 2 else 0)
+ (states[1][i + 3] if i < n - 3 else 0),
)
states[1][i] = min(
states[0][i + 1] if i < n - 1 else 0,
states[0][i + 2] if i < n - 2 else 0,
states[0][i + 3] if i < n - 3 else 0,
)
return states[0][0]
for i in range(int(input())):
n = int(input())
bricks = [int(i) for i in input().split()]
print(game_bricks(bricks, n)) | FUNC_DEF ASSIGN VAR LIST BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def get_memo(n, memo):
if n >= len(memo):
return 0
else:
return memo[n]
def solver(N, bStack):
memo = [0] * N
for n in range(N - 1, -1, -1):
if n == N - 1:
memo[n] = bStack[n]
elif n == N - 2:
memo[n] = sum(bStack[n:])
elif n == N - 3:
memo[n] = sum(bStack[n:])
else:
x = bStack[n] + min(
get_memo(n + 2, memo), get_memo(n + 3, memo), get_memo(n + 4, memo)
)
y = (
bStack[n]
+ bStack[n + 1]
+ min(
get_memo(n + 3, memo), get_memo(n + 4, memo), get_memo(n + 5, memo)
)
)
z = (
bStack[n]
+ bStack[n + 1]
+ bStack[n + 2]
+ min(
get_memo(n + 4, memo), get_memo(n + 5, memo), get_memo(n + 6, memo)
)
)
memo[n] = max(x, y, z)
return memo[0]
T = int(input())
for t in range(T):
N = int(input())
bStack = [int(x) for x in input().split()]
maxScore = solver(N, bStack)
print(maxScore) | FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | import sys
def max_profit(arr):
max_ahead = list(arr)
max_move = list(arr)
L = len(arr)
max_move[L - 1] = 1
max_move[L - 2] = 2
max_move[L - 3] = 3
max_ahead[L - 2] = max_ahead[L - 2] + max_ahead[L - 1]
max_ahead[L - 3] = max_ahead[L - 3] + max_ahead[L - 2]
max_ahead.insert(L, 0)
for i, val in enumerate(reversed(max_ahead)):
pos = L - i
if pos < L - 3:
p = arr[pos] + max_ahead[pos + 1 + max_move[pos + 1]]
q = arr[pos] + arr[pos + 1] + max_ahead[pos + 2 + max_move[pos + 2]]
r = (
arr[pos]
+ arr[pos + 1]
+ arr[pos + 2]
+ max_ahead[pos + 3 + max_move[pos + 3]]
)
max_ahead[pos] = max(p, q, r)
if max_ahead[pos] == p:
max_move[pos] = 1
if max_ahead[pos] == q:
max_move[pos] = 2
if max_ahead[pos] == r:
max_move[pos] = 3
return max_ahead[0]
t = int(input().strip())
for i in range(t):
n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(" ")]
print(max_profit(arr)) | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | test = int(input())
def getOptimalMap(lst, index, dic, sum_array, length):
if dic[index] is not None:
return dic[index]
if length - index <= 3:
dic[index] = sum_array[index]
else:
m = None
for i in range(1, 4):
x = getOptimalMap(lst, index + i, dic, sum_array, length)
val = sum_array[index] - x
if m is None:
m = val
elif val > m:
m = val
dic[index] = m
return dic[index]
while test:
test -= 1
n = int(input())
lst = [int(x) for x in input().strip().split(" ")]
length = len(lst)
dic = [None] * length
dic[length - 1] = lst[-1]
sum_array = [0] * length
tmp = 0
for i in reversed(range(length)):
sum_array[i] = tmp + lst[i]
tmp = sum_array[i]
for i in range(2, len(lst) + 1):
getOptimalMap(lst, length - i, dic, sum_array, length)
print(dic[0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR NONE RETURN VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | from itertools import accumulate
for _ in range(int(input())):
n = int(input())
br = [int(x) for x in input().split()]
br.reverse()
dp = [0] * n
sums = list(accumulate(br))
dp[0] = sums[0]
dp[1] = sums[1]
dp[2] = sums[2]
for i in range(3, n):
dp[i] = max(sums[i] - dp[i - 1], sums[i] - dp[i - 2], sums[i] - dp[i - 3])
print(dp[n - 1]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | def solve(stack):
max_score_at = {len(stack): 0}
for i, num in reversed(list(enumerate(stack))):
possible_scores = []
for n_taken in (1, 2, 3):
if i + n_taken - 1 >= len(stack):
break
score = sum(stack[i : i + n_taken]) - max_score_at[i + n_taken]
possible_scores.append(score)
max_score_at[i] = max(possible_scores)
max_delta = max_score_at[0]
return (max_delta + sum(stack)) // 2
T = int(input())
cases = []
for _ in range(T):
input()
cases.append([int(x) for x in input().split(" ")])
for stack in cases:
print(solve(stack)) | FUNC_DEF ASSIGN VAR DICT FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | t = eval(input())
def max(a, b, c):
result = a
if result < b:
result = b
if result < c:
result = c
return result
while t > 0:
t -= 1
n = eval(input())
arr = list(map(int, input().split()))
sumRight = [0] * (n + 1)
sumRight[n] = 0
for i in reversed(range(n)):
sumRight[i] = arr[i] + sumRight[i + 1]
best = [0] * (n + 1)
best[n] = 0
for i in reversed(range(n)):
temp1 = arr[i] + sumRight[i + 1] - best[i + 1]
temp2 = 0
temp3 = 0
if i <= n - 2:
temp2 = arr[i] + arr[i + 1] + sumRight[i + 2] - best[i + 2]
if i <= n - 3:
temp3 = arr[i] + arr[i + 1] + arr[i + 2] + sumRight[i + 3] - best[i + 3]
best[i] = max(temp1, temp2, temp3)
print(best[0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move.
As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move.
Function Description
Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score.
bricksGame has the following parameter(s):
arr: an array of integers
Input Format
The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.
Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format:
The first line contains an integer $n$, the number of bricks in $\textbf{arr}$.
The next line contains $n$ space-separated integers $arr[i].
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$0\leq arr[i]\leq10^9$
Output Format
For each test case, print a single line containing your maximum score.
Sample Input
2
5
999 1 1 1 0
5
0 1 1 1 999
Sample Output
1001
999
Explanation
In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001.
In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick. | t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
arr = arr[::-1]
p1 = [(0) for x in range(n)]
p2 = [(0) for x in range(n)]
p1[0], p1[1], p1[2], p1[3], p1[4] = (
arr[0],
arr[1] + arr[0],
arr[0] + arr[1] + arr[2],
arr[3] + arr[1] + arr[2],
arr[4] + arr[3] + arr[2] if arr[0] < arr[2] + arr[3] else arr[0] + arr[4],
)
p2[0], p2[1], p2[2], p2[3], p2[4] = (
0,
0,
0,
arr[0],
arr[1] + arr[3] + arr[2] if arr[0] >= arr[2] + arr[3] else arr[0] + arr[1],
)
for i in range(5, n):
a = arr[i] + p2[i - 1]
b = arr[i] + arr[i - 1] + p2[i - 2]
c = arr[i] + arr[i - 1] + arr[i - 2] + p2[i - 3]
if a >= b and a >= c:
p1[i], p2[i] = a, p1[i - 1]
elif a >= b and a < c:
p1[i], p2[i] = c, p1[i - 3]
elif a <= b and b >= c:
p1[i], p2[i] = b, p1[i - 2]
elif a <= b and b <= c:
p1[i], p2[i] = c, p1[i - 3]
else:
print("No match")
print(p1[n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
input = sys.stdin.readline
n = int(input())
c = list(map(int, input().split()))
t = [[] for _ in range(n)]
ab = []
for _ in range(n - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
ab.append([a, b])
t[a].append(b)
t[b].append(a)
root = [0] * n
root[0] = -1
rank = [0] * n
Q = [0]
visited = {0}
ra = 1
while Q:
P = []
for i in Q:
for j in t[i]:
if j in visited:
continue
rank[j] = ra
root[j] = i
visited.add(j)
P.append(j)
Q = P
ra += 1
rankk = [[rank[i], i] for i in range(n)]
rankk.sort()
d = [0] * n
for i in range(n - 1, 0, -1):
k = rankk[i][1]
if c[k] == 0:
d[k] -= 1
else:
d[k] += 1
d[root[k]] += max(0, d[k])
if c[0] == 0:
d[0] -= 1
else:
d[0] += 1
for i in range(1, n):
k = rankk[i][1]
d[k] = max(d[k], min(d[k], 0) + d[root[k]])
print(*d) | 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 LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
def main():
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
adj = [[] for _ in range(N + 1)]
for _ in range(N - 1):
a, b = map(int, input().split())
adj[a].append(b)
adj[b].append(a)
def op(a, b):
return a + max(0, b)
ident_op = 0
def cum_merge(a, b):
return a + b
ident_cum_merge = 0
def Rerooting(adj):
N = len(adj) - 1
st = [1]
seen = [0] * (N + 1)
seen[1] = 1
par = [0] * (N + 1)
child = [[] for _ in range(N + 1)]
seq = []
while st:
v = st.pop()
seq.append(v)
for u in adj[v]:
if not seen[u]:
seen[u] = 1
par[u] = v
child[v].append(u)
st.append(u)
seq.reverse()
dp = [ident_op] * (N + 1)
left = [ident_cum_merge] * (N + 1)
right = [ident_cum_merge] * (N + 1)
for v in seq:
tmp = ident_cum_merge
for u in child[v]:
left[u] = tmp
tmp = op(tmp, dp[u])
tmp = ident_cum_merge
for u in reversed(child[v]):
right[u] = tmp
tmp = op(tmp, dp[u])
dp[v] = tmp
if A[v - 1]:
dp[v] += 1
else:
dp[v] -= 1
seq.reverse()
from_par = [ident_op] * (N + 1)
for v in seq:
if v == 1:
continue
from_par[v] = op(cum_merge(left[v], right[v]), from_par[par[v]])
if A[par[v] - 1]:
from_par[v] += 1
else:
from_par[v] -= 1
dp[v] = op(dp[v], from_par[v])
return dp
dp = Rerooting(adj)
print(*dp[1:])
main() | IMPORT FUNC_DEF 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 LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
input = sys.stdin.readline
getint = lambda: int(input())
getints = lambda: [int(a) for a in input().split()]
n = getint()
colors = [(i * 2 - 1) for i in getints()]
tree = [[] for i in range(n)]
for i in range(n - 1):
a, b = getints()
tree[a - 1].append(b - 1)
tree[b - 1].append(a - 1)
parent = [-1] * n
order = [0]
for i in range(n):
node = order[i]
for child in tree[node]:
if child == parent[node]:
continue
parent[child] = node
order.append(child)
order.reverse()
points = [0] * n
for node in order:
children = tree[node]
point = colors[node]
for child in children:
if child == parent[node]:
continue
point += max(0, points[child])
points[node] = point
order.reverse()
for node in order:
children = tree[node]
for child in children:
if child == parent[node]:
continue
points[child] += max(0, points[node] - max(0, points[child]))
print(" ".join([str(i) for i in points])) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
input = sys.stdin.readline
INF = 10**18
N = int(input())
Color = list(map(int, input().split()))
graph = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = map(int, input().split())
graph[a - 1].append(b - 1)
graph[b - 1].append(a - 1)
Score = [{} for _ in range(N)]
Max = [-INF] * N
for i, c in enumerate(Color):
Max[i] = 1 if c else -1
def dfs(s):
stack = [s]
Ind = [0] * N
while stack:
p = stack[-1]
if Ind[p] == len(graph[p]):
stack.pop()
if stack:
par = stack[-1]
Max[par] += max(Max[p], 0)
elif len(stack) > 1 and graph[p][Ind[p]] == stack[-2]:
Ind[p] += 1
else:
stack.append(graph[p][Ind[p]])
Ind[p] += 1
ans = [-INF] * N
def dfs2(s):
ans[s] = Max[s]
stack = [s]
Ind = [0] * N
while stack:
p = stack[-1]
if Ind[p] == len(graph[p]):
stack.pop()
elif len(stack) > 1 and graph[p][Ind[p]] == stack[-2]:
Ind[p] += 1
else:
ch = graph[p][Ind[p]]
ans[ch] = max(ans[p] - max(Max[ch], 0), 0) + Max[ch]
stack.append(ch)
Ind[p] += 1
dfs(0)
dfs2(0)
print(*ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER 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 LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5 * 2)
n = int(input())
c = [int(item) for item in input().split()]
edge = [[] for _ in range(n)]
for _ in range(n - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
edge[a].append(b)
edge[b].append(a)
memo = [None] * n
def dfs(p, v):
st = []
st.append((p, v, 0))
while st:
p, v, is_return = st.pop()
if is_return:
val = 0
for nv in edge[v]:
if nv == p:
continue
if memo[nv] > 0:
val += memo[nv]
if c[v] == 0:
val = max(val - 1, -1)
else:
val = max(val + 1, 1)
memo[v] = val
else:
st.append((p, v, 1))
for nv in edge[v]:
if nv == p:
continue
st.append((v, nv, 0))
def dfs_dp(p, v):
st = []
st.append((p, v))
while st:
p, v = st.pop()
val = memo[v]
if val < 0:
up = memo[p]
else:
up = memo[p] - memo[v]
if up > 0:
val += up
if p != -1:
memo[v] = val
for nv in edge[v]:
if nv == p:
continue
st.append((v, nv))
dfs(-1, 0)
dfs_dp(-1, 0)
print(*memo) | IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
class Tree:
def __init__(self, n, decrement=1):
self.n = n
self.edges = [[] for _ in range(n)]
self.root = None
self.size = [1] * n
self.decrement = decrement
def add_edge(self, u, v):
u, v = u - self.decrement, v - self.decrement
self.edges[u].append(v)
self.edges[v].append(u)
def add_edges(self, edges):
for u, v in edges:
u, v = u - self.decrement, v - self.decrement
self.edges[u].append(v)
self.edges[v].append(u)
def set_root(self, root):
root -= self.decrement
self.depth = [-1] * self.n
self.root = root
self.par = [-1] * self.n
self.depth[root] = 0
self.order = [root]
next_set = [root]
while next_set:
p = next_set.pop()
for q in self.edges[p]:
if self.depth[q] != -1:
continue
self.par[q] = p
self.depth[q] = self.depth[p] + 1
self.order.append(q)
next_set.append(q)
for p in self.order[::-1]:
for q in self.edges[p]:
if self.par[p] == q:
continue
self.size[p] += self.size[q]
def rerooting(self, op, merge, id):
dp1 = [id] * self.n
dp2 = [id] * self.n
for p in self.order[::-1]:
t = id
for q in self.edges[p]:
if self.par[p] == q:
continue
dp2[q] = t
t = merge(t, op(dp1[q], p, q))
t = id
for q in self.edges[p][::-1]:
if self.par[p] == q:
continue
dp2[q] = merge(t, dp2[q])
t = merge(t, op(dp1[q], p, q))
dp1[p] = t
for q in self.order[1:]:
pq = self.par[q]
dp2[q] = op(merge(dp2[q], dp2[pq]), q, pq)
dp1[q] = merge(dp1[q], dp2[q])
return dp1
input = sys.stdin.readline
N = int(input())
T = Tree(N)
C = list(map(int, input().split()))
for _ in range(N - 1):
x, y = map(int, input().split())
T.add_edge(x, y)
T.set_root(1)
def op(a, p, q):
return max(a + 2 * C[q] - 1, 0)
def merge(a, b):
return a + b
id = 0
dp = T.rerooting(op, merge, id)
res = [""] * N
for i in range(N):
res[i] = str(dp[i] + 2 * C[i] - 1)
print(" ".join(res)) | IMPORT CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | class WhiteSubtree:
def __init__(self, vertex_is_white, edges, root=1):
self.vertex_is_white = vertex_is_white
self.root = root
self.adj = self.__build_adj_list__(edges)
self.subtree_best_polarity = self.__compute_subtree_best_polarity__()
self.score_at_nodes = self.__compute_final_score_at_nodes__()
def __build_adj_list__(self, edges):
n = len(self.vertex_is_white) - 1
adj = [[] for _ in range(n + 1)]
adj[0] = None
for v1, v2 in edges:
adj[v1].append(v2)
adj[v2].append(v1)
return adj
def __compute_subtree_best_polarity__(self):
n = len(self.vertex_is_white) - 1
subtree_best_polarity = [None for _ in range(n + 1)]
visited = [(False) for _ in range(n + 1)]
stack = [(self.root, None)]
while len(stack) > 0:
u, up = stack[-1]
if not visited[u]:
for v in self.adj[u]:
if v != up:
stack.append((v, u))
visited[u] = True
else:
node_score = 1 if self.vertex_is_white[u] else -1
for v in self.adj[u]:
if v != up:
node_score += max(0, subtree_best_polarity[v])
subtree_best_polarity[u] = node_score
stack.pop()
return subtree_best_polarity
def __compute_final_score_at_nodes__(self):
subtree_best_polarity = self.subtree_best_polarity
n = len(self.vertex_is_white) - 1
score_at_nodes = [None for _ in range(n + 1)]
stack = [(self.root, None, 0)]
while len(stack) > 0:
u, up, contrib_from_parent = stack.pop()
score_at_nodes[u] = subtree_best_polarity[u] + contrib_from_parent
for v in self.adj[u]:
if v != up:
contrib_to_parent = max(subtree_best_polarity[v], 0)
stack.append((v, u, max(score_at_nodes[u] - contrib_to_parent, 0)))
return score_at_nodes
def construct_tree():
n = int(input())
vertex_is_white = [(int(elm.strip()) == 1) for elm in input().split()]
vertex_is_white = [[]] + vertex_is_white
edges = []
for _ in range(n - 1):
v1, v2 = [int(elm.strip()) for elm in input().split()]
edges.append((v1, v2))
return WhiteSubtree(vertex_is_white, edges)
tree = construct_tree()
scores = tree.score_at_nodes[1:]
print(" ".join([str(elm) for elm in scores])) | CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NONE FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NONE WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NONE NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
sys.setrecursionlimit(10**6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def MI1():
return map(int1, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
def main():
def dfs():
stack = [(0, -1, False)]
while stack:
u, pu, re = stack.pop()
if re:
dp1[u] += (
aa[u] * 2
- 1
+ sum(dp1[cu] for cu in to[u] if cu != pu and dp1[cu] > 0)
)
else:
stack.append((u, pu, True))
for cu in to[u]:
if cu == pu:
continue
stack.append((cu, u, False))
def dfs2():
stack = [(0, -1)]
while stack:
u, pu = stack.pop()
ans[u] = (
sum(dp1[cu] for cu in to[u] if cu != pu and dp1[cu] > 0) + aa[u] * 2 - 1
)
if u and dp2[u] > 0:
ans[u] += dp2[u]
for cu in to[u]:
if cu == pu:
continue
d = dp1[cu] if dp1[cu] > 0 else 0
dp2[cu] = ans[u] - d
stack.append((cu, u))
n = II()
aa = LI()
to = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = MI1()
to[u].append(v)
to[v].append(u)
dp1 = [0] * n
dfs()
dp2 = [0] * n
ans = [0] * n
dfs2()
print(*ans)
main() | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n = int(input())
a = list(map(int, input().split()))
G = [[] for i in range(n)]
for i in range(n - 1):
x, y = map(int, input().split())
x, y = x - 1, y - 1
G[x].append(y)
G[y].append(x)
cost = [(2 * i - 1) for i in a]
def bfs1(root):
rev = [(root, -1)]
for i in range(n):
cur, par = rev[i]
for to in G[cur]:
if to != par:
rev.append((to, cur))
for i in reversed(range(1, n)):
cur, par = rev[i]
if cost[cur] > 0:
cost[par] += cost[cur]
cost2 = [(2 * i - 1) for i in a]
def bfs2(root):
rev = [(root, -1, 0)]
for i in range(n):
cur, par, d_par = rev[i]
d_nxt_tmp = d_par + cost2[cur]
tmp = []
for to in G[cur]:
if to != par:
c = cost[to] if cost[to] > 0 else 0
tmp.append(c)
cost2[cur] += sum(tmp) + d_par
sum1 = [0]
for j in tmp:
sum1.append(sum1[-1] + j)
sum2 = [0]
for j in reversed(tmp):
sum2.append(sum2[-1] + j)
m = len(sum2)
i = 0
for to in G[cur]:
if to != par:
d_nxt = d_nxt_tmp + sum1[i] + sum2[m - 2 - i]
if d_nxt < 0:
d_nxt = 0
rev.append((to, cur, d_nxt))
i += 1
root = n // 2
bfs1(root)
bfs2(root)
print(*cost2) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR 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 LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | def main():
n = int(input())
a = list(map(int, input().split()))
g = [list() for _ in range(n)]
for _ in range(n - 1):
u, v = map(int, input().split())
g[u - 1].append(v - 1)
g[v - 1].append(u - 1)
st = [(x * 2 - 1) for x in a] + [0]
p = [-1] * n
q = [0]
while q:
v = q.pop()
for x in g[v]:
if x == p[v]:
continue
p[x] = v
q.append(x)
seen = [0] * n
q = [0]
while q:
v = q[-1]
if seen[v]:
q.pop()
if st[v] > 0:
st[p[v]] += st[v]
else:
for x in g[v]:
if x == p[v]:
continue
q.append(x)
seen[v] = 1
seen = [0] * n
q = [0]
while q:
v = q.pop()
for x in g[v]:
if x == p[v]:
continue
c = st[v]
if st[x] > 0:
c -= st[x]
if c > 0:
st[x] += c
q.append(x)
return st[:n]
print(*main()) | 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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
A = [(-1 if a == 0 else 1) for a in A]
g = [[] for i in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
u, v = u - 1, v - 1
g[u].append(v)
g[v].append(u)
s = []
s.append(0)
parent = [-1] * n
order = []
while s:
v = s.pop()
order.append(v)
for u in g[v]:
if parent[v] == u:
continue
parent[u] = v
s.append(u)
order.reverse()
dp = [0] * n
for v in order:
dp[v] += A[v]
u = parent[v]
if u == -1:
continue
dp[u] += max(0, dp[v])
ans = [0] * n
order.reverse()
for v in order:
ans[v] = dp[v]
for u in g[v]:
if parent[v] == u:
continue
dp[u] += max(0, ans[v] - max(0, dp[u]))
print(*ans) | 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 VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
def go():
n = int(input())
a = list(map(int, input().split()))
e = {i: set() for i in range(n)}
for _ in range(n - 1):
u, v = map(int, input().split())
u, v = u - 1, v - 1
e[u].add(v)
e[v].add(u)
ranks = [len(e[i]) for i in range(n)]
leafs = [i for i in range(n) if ranks[i] == 1]
index = 0
vs = {i: {} for i in range(n)}
done = set()
while index < len(leafs):
cur = leafs[index]
mysum = sum(vs[cur].values())
if a[cur] == 0:
mysum -= 1
else:
mysum += 1
for i in e[cur] - done:
vs[i][cur] = max(mysum, 0)
ranks[i] -= 1
if ranks[i] == 1:
leafs.append(i)
done.add(cur)
index += 1
sums = [0] * n
que = [ranks.index(0)]
done = set()
index = 0
while index < len(que):
cur = que[index]
mysum = sum(vs[cur].values())
if a[cur] == 0:
mysum -= 1
else:
mysum += 1
sums[cur] = mysum
for i in e[cur] - done:
vs[i][cur] = max(mysum - vs[cur][i], 0)
que.append(i)
done.add(cur)
index += 1
return " ".join(map(str, sums))
for _ in range(1):
print(go()) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR DICT VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | import sys
input = sys.stdin.readline
n = int(input())
A = [0] + list(map(int, input().split()))
for i in range(n + 1):
if A[i] == 0:
A[i] = -1
E = [[] for i in range(n + 1)]
for i in range(n - 1):
x, y = map(int, input().split())
E[x].append(y)
E[y].append(x)
DP_C = [0] * (n + 1)
DP_G = [0] * (n + 1)
Q = [1]
USE = [0] * (n + 1)
USE[1] = 1
Q2 = []
P = [-1] * (n + 1)
SORTED = []
while Q:
x = Q.pop()
SORTED.append(x)
for to in E[x]:
if len(E[to]) == 1 and USE[to] == 0:
SORTED.append(to)
P[to] = x
USE[to] = 1
elif USE[to] == 0:
USE[to] = 1
Q.append(to)
P[to] = x
for x in SORTED[::-1]:
DP_C[x] = max(0, DP_C[x] + A[x])
if P[x] == -1:
continue
DP_C[P[x]] += max(0, DP_C[x])
for x in SORTED:
SUM = DP_G[x]
for to in E[x]:
if P[x] == to:
continue
SUM += DP_C[to]
for to in E[x]:
if P[x] == to:
continue
DP_G[to] = max(0, SUM - DP_C[to] + A[x])
ANS = []
SC = A[1]
for to in E[1]:
SC += DP_C[to]
ANS.append(SC)
for i in range(2, n + 1):
SC = A[i]
SC += max(0, DP_G[i])
for to in E[i]:
if P[i] == to:
continue
SC += max(0, DP_C[to])
ANS.append(SC)
print(*ANS) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).
You have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i$ is the color of the $i$-th vertex.
Each of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \le u_i, v_i \le n, u_i \ne v_i$).
It is guaranteed that the given edges form a tree.
-----Output-----
Print $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.
-----Examples-----
Input
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
Output
2 2 2 2 2 1 1 0 2
Input
4
0 0 1 0
1 2
1 3
1 4
Output
0 -1 1 -1
-----Note-----
The first example is shown below:
[Image]
The black vertices have bold borders.
In the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$. | from sys import setrecursionlimit, stdin
n = int(stdin.readline())
ay = [int(x) for x in stdin.readline().split()]
graph = [set() for x in range(n)]
for e in range(n - 1):
a, b = [(int(x) - 1) for x in stdin.readline().split()]
graph[a].add(b)
graph[b].add(a)
dists = {}
child = {}
bruh = sum(ay)
path = [(0, 0)]
for x, p in path:
for y in graph[x]:
if y != p:
path.append((y, x))
for x, parent in path[::-1]:
total = 0
children = ay[x]
for v in graph[x]:
if v != parent:
c, sm = child[v], dists[v]
children += c
total += sm + c
dists[x] = total
child[x] = children
b2, b3 = child[0], dists[0]
for x, parent in path:
for v in graph[x]:
if v != parent:
dists[v] = dists[x] + bruh - child[v] * 2
print(max([dists[y] for y in range(n)])) | 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 FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | elem = int(input())
ar = []
for i in input().split(" "):
ar.append(int(i))
res = [0]
cont = [0]
gym = [0]
for i in ar:
if i == 0:
res.append(min(res[-1], cont[-1], gym[-1]) + 1)
cont.append(cont[-1] + 1)
gym.append(gym[-1] + 1)
elif i == 1:
temp = min(res[-1], cont[-1], gym[-1]) + 1
temp2 = min(gym[-1], res[-1])
temp3 = gym[-1] + 1
res.append(temp)
cont.append(temp2)
gym.append(temp3)
elif i == 2:
temp = min(res[-1], cont[-1], gym[-1]) + 1
temp2 = cont[-1] + 1
temp3 = min(cont[-1], res[-1])
res.append(temp)
cont.append(temp2)
gym.append(temp3)
else:
temp = min(res[-1], cont[-1], gym[-1]) + 1
temp2 = min(gym[-1], res[-1])
temp3 = min(cont[-1], res[-1])
res.append(temp)
cont.append(temp2)
gym.append(temp3)
print(min(res[-1], cont[-1], gym[-1])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | def min_rest_days(days):
cache = {(-1): [0, 0, 0]}
for i in range(len(days)):
prev_cache = cache[i - 1]
if days[i] == 0:
cache[i] = [
float("inf"),
float("inf"),
min(prev_cache[0], prev_cache[1], prev_cache[2]) + 1,
]
elif days[i] == 1:
if i > 0 and days[i] == days[i - 1]:
prev_min = prev_cache[2]
else:
prev_min = min(prev_cache[0], prev_cache[2])
cache[i] = [
float("inf"),
prev_min,
min(prev_cache[0], prev_cache[1], prev_cache[2]) + 1,
]
elif days[i] == 2:
if i > 0 and days[i] == days[i - 1]:
prev_min = prev_cache[2]
else:
prev_min = min(prev_cache[1], prev_cache[2])
cache[i] = [
prev_min,
float("inf"),
min(prev_cache[0], prev_cache[1], prev_cache[2]) + 1,
]
elif days[i] == 3:
first = min(prev_cache[1], prev_cache[2])
second = min(prev_cache[0], prev_cache[2])
if i > 0:
if days[i - 1] == 1:
second = prev_cache[2]
elif days[i - 1] == 2:
first = prev_cache[2]
cache[i] = [
first,
second,
min(prev_cache[0], prev_cache[1], prev_cache[2]) + 1,
]
return min(cache[len(days) - 1])
_ = input()
days = [int(el) for el in input().split()]
print(min_rest_days(days)) | FUNC_DEF ASSIGN VAR DICT NUMBER LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | t = int(input())
s = list(map(int, input().split()))
dp1 = [(0) for i in range(t)]
dp2 = [(0) for i in range(t)]
dp1[0] = 0 if s[0] & 1 else 1
dp2[0] = 0 if s[0] & 2 else 1
for i in range(1, t):
if s[i] == 3:
dp1[i], dp2[i] = dp2[i - 1], dp1[i - 1]
elif s[i] == 2:
dp1[i], dp2[i] = min(dp1[i - 1], dp2[i - 1]) + 1, dp1[i - 1]
elif s[i] == 1:
dp1[i], dp2[i] = dp2[i - 1], min(dp1[i - 1], dp2[i - 1]) + 1
else:
dp2[i] = dp1[i] = min(dp1[i - 1], dp2[i - 1]) + 1
print(min(dp1[t - 1], dp2[t - 1])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | import sys
input = sys.stdin.readline
n = int(input())
l = list(map(int, input().split()))
dp = [[float("inf") for i in range(3)] for i in range(n)]
dp[0][2] = 1
if l[0] == 3:
dp[0][0] = 0
dp[0][1] = 0
elif l[0] == 2:
dp[0][0] = 0
elif l[0] == 1:
dp[0][1] = 0
for i in range(1, n):
cur = l[i]
dp[i][2] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + 1
if cur == 1:
dp[i][1] = min(dp[i - 1][0], dp[i - 1][2])
elif cur == 2:
dp[i][0] = min(dp[i - 1][1], dp[i - 1][2])
elif cur == 3:
dp[i][0] = min(dp[i - 1][1], dp[i - 1][2])
dp[i][1] = min(dp[i - 1][0], dp[i - 1][2])
print(min(dp[n - 1][0], dp[n - 1][1], dp[n - 1][2])) | 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 STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
data = list(map(int, input().split()))
ans = 0
prev = 0
i = 0
while i < n:
if data[i] is 0:
ans = ans + 1
prev = 0
elif data[i] is 1:
if prev is 1:
ans += 1
prev = 0
else:
prev = 1
elif data[i] is 2:
if prev is 2:
ans += 1
prev = 0
else:
prev = 2
elif prev is 1:
prev = 2
elif prev is 2:
prev = 1
else:
j = i
while j < n:
if data[j] is not 3:
i = j - 1
break
j += 1
i += 1
print(ans) | 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 NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
A = list(map(int, input().split()))
f1 = 0
f2 = 0
t = 0
a = 0
for i in range(n):
if A[i] == 0:
a += 1
f1 = 0
f2 = 0
elif A[i] == 1:
if f1 == 1:
a += 1
f1 = 0
else:
f1 = 1
f2 = 0
elif A[i] == 2:
if f2 == 1:
a += 1
f2 = 0
else:
f1 = 0
f2 = 1
elif A[i] == 3:
if f1 == 1:
f1 = 0
f2 = 1
elif f2 == 1:
f1 = 1
f2 = 0
print(a) | 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | def dp(prev, ind):
nonlocal dar, n, con, gym
if ind >= n:
return 0
if (prev, ind) not in dar:
m = dp("free", ind + 1) + 1
if prev != "con" and con[ind]:
m = min(m, dp("con", ind + 1))
if prev != "gym" and gym[ind]:
m = min(m, dp("gym", ind + 1))
dar[prev, ind] = m
return dar[prev, ind]
dar = {}
n = int(input())
L = [int(x) for x in input().split()]
con = [(x & 1) for x in L]
gym = [(x & 2) for x in L]
print(dp("free", 0)) | FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER IF VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | _ = input()
days = map(int, input().split())
off = 0
last = 0
for d in days:
if d == 0:
off += 1
last = 0
elif last == 0 or last == 3:
last = d
elif d in {3, 3 - last}:
last = 3 - last
else:
off += 1
last = 0
print(off) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
dias = list(map(int, input().split()))
OPT = [([0] * 3) for i in range(2)]
for i in range(n):
if dias[i] == 0:
optimo_anterior = 1 + OPT[0][0]
OPT[1][0] = optimo_anterior
OPT[1][1] = optimo_anterior
OPT[1][2] = optimo_anterior
elif dias[i] == 1:
optimo_anterior = min(OPT[0][0] + 1, OPT[0][2])
OPT[1][0] = optimo_anterior
OPT[1][1] = optimo_anterior
OPT[1][2] = 1 + OPT[0][0]
elif dias[i] == 2:
optimo_anterior = min(OPT[0][0] + 1, OPT[0][1])
OPT[1][0] = optimo_anterior
OPT[1][2] = optimo_anterior
OPT[1][1] = 1 + OPT[0][0]
else:
OPT[1][0] = min(OPT[0][0] + 1, OPT[0][1], OPT[0][2])
OPT[1][1] = min(OPT[0][0] + 1, OPT[0][2])
OPT[1][2] = min(OPT[0][0] + 1, OPT[0][1])
OPT[0][0] = OPT[1][0]
OPT[0][1] = OPT[1][1]
OPT[0][2] = OPT[1][2]
print(OPT[1][0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
l = list(map(int, input().split()))
cur = ans = 0
for a in l:
if a == 0:
ans += 1
cur = 0
elif a == 1:
if cur in (0, 1):
cur = 2
else:
ans += 1
cur = 0
elif a == 2:
if cur in (0, 2):
cur = 1
else:
ans += 1
cur = 0
else:
cur = [0, 2, 1][cur]
print(ans) | 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 NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | def __starting_point():
MAX = 100
n = int(input())
num = list()
for i in range(n + 1):
num.append([0, 0, 0])
line = str(input()).split()
for i in range(n):
if line[i] == "0":
num[i + 1][0] = 1 + min(num[i])
num[i + 1][1] = MAX
num[i + 1][2] = MAX
elif line[i] == "1":
num[i + 1][0] = 1 + min(num[i])
num[i + 1][1] = min(num[i][0], num[i][2])
num[i + 1][2] = MAX
elif line[i] == "2":
num[i + 1][0] = 1 + min(num[i])
num[i + 1][1] = MAX
num[i + 1][2] = min(num[i][0], num[i][1])
elif line[i] == "3":
num[i + 1][0] = 1 + min(num[i])
num[i + 1][1] = min(num[i][0], num[i][2])
num[i + 1][2] = min(num[i][0], num[i][1])
print(min(num[n]))
__starting_point() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
dp = [[(0) for i in range(3)] for j in range(n + 1)]
a = list(map(int, input().split()))
for i in range(1, n + 1):
if a[i - 1] == 1 or a[i - 1] == 3:
dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + 1
if a[i - 1] == 2 or a[i - 1] == 3:
dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + 1
dp[i][0] = max(max(dp[i - 1][0], dp[i - 1][1]), dp[i - 1][2])
print(n - max(max(dp[n][0], dp[n][1]), dp[n][2])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | import sys
input = lambda: sys.stdin.readline()
int_arr = lambda: list(map(int, input().split()))
str_arr = lambda: list(map(str, input().split()))
get_str = lambda: map(str, input().split())
get_int = lambda: map(int, input().split())
get_flo = lambda: map(float, input().split())
mod = 1000000007
def solve(n, arr):
prev = 0
c = 0
for i in range(n):
if arr[i] == prev:
c += 1
prev = 0
elif arr[i] == 0:
c += 1
prev = 0
elif arr[i] == 1 or arr[i] == 2:
prev = arr[i]
elif prev:
prev = 3 - prev
print(c)
n = int(input())
arr = int_arr()
solve(n, arr) | IMPORT ASSIGN 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
a = list(map(int, input().split()))
d = [([10**6] * 3) for i in range(n)]
d[0][0] = 1
if a[0] & 1:
d[0][1] = 0
if a[0] & 2:
d[0][2] = 0
for i in range(1, n):
d[i][0] = min(d[i - 1]) + 1
if a[i] & 1:
d[i][1] = min(d[i - 1][0], d[i - 1][2])
if a[i] & 2:
d[i][2] = min(d[i - 1][0], d[i - 1][1])
print(min(d[-1])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
a = list(map(int, input().split()))
cnt = 0
last = a[0]
if last == 3:
last = 5
if a[0] != 0:
cnt += 1
for i in range(1, n):
if a[i] != last and a[i] != 0:
if a[i] == 3:
if last == 0 or last == 5:
last = 5
cnt += 1
else:
last = abs(last - 3)
cnt += 1
else:
last = a[i]
cnt += 1
else:
last = 0
print(n - cnt) | 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 VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
a = list(map(int, input().split(" ")))
l = [3, 2, 1, 3]
rest = 0
last = 0
for i in range(n):
if a[i] == 0:
rest += 1
last = 0
elif a[i] == 3:
last = l[last]
elif last == a[i]:
rest += 1
last = 0
else:
last = a[i]
print(rest) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | def get_ints(string):
return list(map(int, string.split()))
def get_input():
n = int(input())
xs = get_ints(input())
return n, xs
def foo(xs, res, last):
if len(xs) is []:
return res
for i, x in enumerate(xs):
if x == 0:
res += 1
last = 0
elif x == 1 and last == 1:
res += 1
last = 0
elif x == 1:
last = 1
elif x == 2 and last == 2:
res += 1
last = 0
elif x == 2:
last = 2
elif last == 1:
last = 2
elif last == 2:
last = 1
elif i < len(xs) - 1 and xs[i + 1] == 0:
last = 1
else:
return min(foo(xs[i + 1 :], res, 1), foo(xs[i + 1 :], res, 2))
return res
def main():
n, xs = get_input()
res = foo(xs, 0, 0)
print(res)
return res
def __starting_point():
main()
__starting_point() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR LIST RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | input()
l = list(map(int, input().split()))
r = 0
prev = 3
for i in l:
if i == prev and prev != 3:
i = 0
elif i == 3 and prev != 3:
i -= prev
if i == 0:
r = r + 1
prev = i
print(r) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
a = [0] + list(map(int, input().split()))
vix = 0
for i in range(1, n + 1):
if a[i] == 0:
vix += 1
elif a[i] == 3:
a[i] = 3 - a[i - 1]
elif a[i] == a[i - 1]:
vix += 1
a[i] = 0
print(vix) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | user = int(input())
n = list(input().split(" "))
l = len(n) - 1
if int(n[0]) == 3 and int(n[l]) != 3:
n.reverse()
r = 0
a = 0
c = 0
res = []
for i in n:
if int(i) == 0:
r = r + 1
a = 0
c = c + 1
if int(i) == 1:
if a == 0 or a != 2:
a = 2
c = c + 1
else:
r = r + 1
a = 0
c = c + 1
if int(i) == 2:
if a == 0 or a != 1:
a = 1
c = c + 1
else:
r = r + 1
a = 0
c = c + 1
if int(i) == 3:
if a == 0:
while c < len(n) and n[c] == 3:
c = c + 1
if c < len(n):
if n[c] == 1:
a = 2
elif n[c] == 2:
a = 1
elif a != 1:
a = 1
elif a != 2:
a = 2
print(r) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
k = list(map(int, input().split(" ")))
ans = 0
last = "n"
for i in k:
if i == 0:
now = "n"
elif i == 1 and last != "p":
now = "p"
elif i == 1:
now = "n"
elif i == 2 and last != "s":
now = "s"
elif i == 2:
now = "n"
elif last == "s":
now = "p"
elif last == "p":
now = "s"
else:
now = "a"
if now == "n":
ans += 1
last = now
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR STRING ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR STRING ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
numbers = list(map(int, input().split(" ")))
memo = [[(0) for _ in range(4)] for _ in range(n)]
first = numbers[0]
if first != 0:
if first == 3:
memo[0][1] = 1
memo[0][2] = 1
else:
memo[0][first] = 1
for i in range(1, n):
j = numbers[i]
memo[i][0] = max(memo[i - 1])
if j == 1 or j == 3:
memo[i][1] = max(memo[i - 1][2], memo[i - 1][0]) + 1
if j == 2 or j == 3:
memo[i][2] = max(memo[i - 1][1], memo[i - 1][0]) + 1
print(n - max(memo[n - 1])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | R = lambda: map(int, input().split())
n = int(input())
dp = [[0, 0, 0] for i in range(n + 1)]
for i, x in enumerate(R()):
dp[i][0] = min(dp[i - 1]) + 1
if x == 3:
dp[i][1] = min(dp[i - 1][0], dp[i - 1][2])
dp[i][2] = min(dp[i - 1][0], dp[i - 1][1])
elif x == 2:
dp[i][1] = n + 1
dp[i][2] = min(dp[i - 1][0], dp[i - 1][1])
elif x == 1:
dp[i][1] = min(dp[i - 1][0], dp[i - 1][2])
dp[i][2] = n + 1
else:
dp[i] = [min(dp[i - 1]) + 1] * 3
print(min(dp[n - 1])) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
a = list(map(int, input().split()))
c = ct = gm = 0
for i in range(n):
if a[i] == 0:
ct = 0
gm = 0
c += 1
elif a[i] == 1 and ct == 0:
ct = 1
gm = 0
elif a[i] == 2 and gm == 0:
gm = 1
ct = 0
elif a[i] == 3:
if ct == 1:
ct = 0
gm = 1
elif gm == 1:
gm = 0
ct = 1
else:
ct = 0
gm = 0
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 VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
a = list(map(int, input().split()))
mem = {}
def solve(i, previous):
if i == n:
return 0
if a[i] == 0 or a[i] == previous:
return solve(i + 1, 0) + 1
elif a[i] == 3:
if previous == 0:
return solve(i + 1, 0)
return solve(i + 1, 2 if previous == 1 else 1)
return solve(i + 1, a[i])
print(solve(0, 0)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | n = int(input())
l = list(map(int, input().split()))
output = 1 if l[0] == 0 else 0
u = False
for i in range(1, n):
if l[i] == 0:
output += 1
elif l[i] == l[i - 1] and l[i] != 3:
output += 1
l[i] = 0
elif l[i] == 3:
if l[i - 1] == 1:
l[i] = 2
elif l[i - 1] == 2:
l[i] = 1
print(output) | 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 NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
-----Input-----
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 3) separated by space, where:
a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
-----Output-----
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
to do sport on any two consecutive days, to write the contest on any two consecutive days.
-----Examples-----
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
-----Note-----
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. | 3
n = int(input())
a = list(map(int, input().split()))
dp = [([-1791791791] * 4) for i in range(n)]
dp[0][0] = 0
if a[0] & 1:
dp[0][1] = 1
if a[0] & 2:
dp[0][2] = 1
for i in range(1, n):
dp[i][0] = max(dp[i - 1])
for j in range(1, 3):
if a[i] & j:
dp[i][j] = max(dp[i - 1][:j] + dp[i - 1][j + 1 :]) + 1
ans = max(dp[-1])
print(n - ans) | EXPR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.