description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | c = 0
n = int(input())
s = list(input())
posible = ["B", "G", "R"]
if n != 1:
for i in range(1, n - 1):
if s[i] == s[i - 1]:
if s[i] == "R" and s[i + 1] != "B":
s[i] = "B"
elif s[i] == "R" and s[i + 1] != "G":
s[i] = "G"
elif s[i] == "G" and s[i + 1] != "B":
s[i] = "B"
elif s[i] == "G" and s[i + 1] != "R":
s[i] = "R"
elif s[i] == "B" and s[i + 1] != "R":
s[i] = "R"
elif s[i] == "B" and s[i + 1] != "G":
s[i] = "G"
c += 1
if s[-1] == s[-2]:
c += 1
for i in posible:
if i != s[-1]:
s[-1] = i
break
else:
c = 0
print(c)
print("".join(s)) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
s = list(input())
rgb = ["R", "B", "G"]
x = 0
if n == 1:
print(0)
print(s[0])
exit(0)
for i in range(len(s) - 2):
if s[i] == s[i + 1] and s[i] != s[i + 2]:
rgb.remove(s[i])
rgb.remove(s[i + 2])
s[i + 1] = rgb[0]
x += 1
elif s[i] == s[i + 1]:
rgb.remove(s[i])
s[i + 1] = rgb[0]
x += 1
rgb = ["R", "B", "G"]
if s[len(s) - 2] == s[len(s) - 1]:
rgb.remove(s[len(s) - 2])
s[len(s) - 1] = rgb[0]
x += 1
print(x)
print("".join(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST STRING STRING STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
s = list(input())
so = s[0]
COUNT = 0
for i in range(1, n):
if s[i] != so:
so = s[i]
continue
elif i != n - 1:
if so == "R" and s[i + 1] == "R":
s[i] = "G"
if so == "G" and s[i + 1] == "G":
s[i] = "B"
if so == "B" and s[i + 1] == "B":
s[i] = "R"
if so == "R" and s[i + 1] == "G" or so == "G" and s[i + 1] == "R":
s[i] = "B"
if so == "R" and s[i + 1] == "B" or so == "B" and s[i + 1] == "R":
s[i] = "G"
if so == "B" and s[i + 1] == "G" or so == "G" and s[i + 1] == "B":
s[i] = "R"
so = s[i]
COUNT += 1
else:
COUNT += 1
if s[i - 1] == "R" or s[i - 1] == "B":
s[i] = "G"
if s[i - 1] == "G":
s[i] = "R"
print(COUNT)
print("".join(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR STRING VAR BIN_OP VAR NUMBER STRING VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR STRING VAR BIN_OP VAR NUMBER STRING VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR STRING VAR BIN_OP VAR NUMBER STRING VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
s = input()
ar = list(s)
k = 0
if n == 1:
print(0)
print(s)
elif n == 2:
if len(set(s)) == 1:
if ar[0] == "R":
print(1)
print("RG")
elif ar[0] == "G":
print(1)
print("GB")
else:
print(1)
print("BR")
else:
print(0)
print(s)
else:
for i in range(1, n - 1):
if ar[i] == ar[i - 1]:
if ar[i] == "R":
if ar[i + 1] == "G":
ar[i] = "B"
else:
ar[i] = "G"
elif ar[i] == "G":
if ar[i + 1] == "B":
ar[i] = "R"
else:
ar[i] = "B"
elif ar[i + 1] == "R":
ar[i] = "G"
else:
ar[i] = "R"
k = k + 1
if ar[-1] == ar[-2]:
if ar[-2] == "R":
ar[-1] = "G"
elif ar[-2] == "G":
ar[-1] = "R"
else:
ar[-1] = "G"
k = k + 1
print(k)
print("".join(ar)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
s = "!" + input().strip()
dp = [([10**6] * (n + 1)) for i in range(3)]
pr = [([None] * (n + 1)) for i in range(3)]
dp[0][0] = dp[1][0] = dp[2][0] = 0
for i in range(1, n + 1):
for c in range(3):
op1 = dp[(c + 1) % 3][i - 1]
op2 = dp[(c + 2) % 3][i - 1]
pr[c][i] = (c + 1) % 3 if op1 < op2 else (c + 2) % 3
dp[c][i] = min(op1, op2) + ("RGB".index(s[i]) != c)
ans = []
end = [dp[0][n], dp[1][n], dp[2][n]]
c = end.index(min(end))
for i in range(n, 0, -1):
ans.append(c)
c = pr[c][i]
print(min(end))
print("".join("RGB"[c] for c in ans[::-1])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL STRING VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING STRING VAR VAR VAR NUMBER |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | def main():
n = int(input())
clone = set(["R", "G", "B"])
lamps = [i for i in input()]
c = 0
for i in range(len(lamps) - 1):
if lamps[i] == lamps[i + 1] and i != len(lamps) - 2:
t = clone - set([lamps[i], lamps[i + 2]])
lamps[i + 1] = t.pop()
c += 1
if lamps[i] == lamps[i + 1] and i == len(lamps) - 2:
t = clone - set([lamps[i]])
lamps[i + 1] = t.pop()
c += 1
print(c)
print("".join(lamps))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | import sys
input = sys.stdin.readline
n = int(input())
s = input()
if n == 1:
print(0)
print(s)
exit()
answer = ["0"] * n
shomarande = 1
answer[0] = s[0]
a = 0
b = 0
if s[1] == s[0]:
b = 1
for i in range(2, n):
if s[i] == s[i - 1]:
c = min(a, b) + 1
a, b = b, c
if s[i] != answer[shomarande - 1]:
answer[shomarande] = s[i]
elif s[i] != "B":
answer[shomarande] = "B"
else:
answer[shomarande] = "R"
else:
if s[i - 1] == answer[shomarande - 1]:
o = [s[i], answer[shomarande - 1]]
if "G" not in o:
answer[shomarande] = "G"
elif "R" not in o:
answer[shomarande] = "R"
else:
answer[shomarande] = "B"
else:
answer[shomarande] = s[i - 1]
a = b
shomarande += 1
if s[n - 1] == answer[shomarande - 1]:
if answer[shomarande - 1] != "B":
answer[shomarande] = "B"
else:
answer[shomarande] = "G"
else:
answer[shomarande] = s[n - 1]
print(b)
print("".join(answer)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR VAR BIN_OP VAR NUMBER IF STRING VAR ASSIGN VAR VAR STRING IF STRING VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
l = list(input()) + ["l"]
D = ["R", "B", "G"]
c = 0
for i in range(1, n):
a = 0
if l[i] == l[i - 1]:
c += 1
l[i] = D[a]
while l[i] == l[i - 1] or l[i] == l[i + 1]:
a += 1
l[i] = D[a]
print(c)
print("".join(l[:n])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST STRING ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
s = list(input())
dp = [[(0) for i in range(3)] for i in range(n)]
rec = {"R": 0, "G": 1, "B": 2}
recInv = {(0): "R", (1): "G", (2): "B"}
dp[0][rec[s[0]]] = 0
dp[0][(rec[s[0]] - 1) % 3] = 1
dp[0][(rec[s[0]] + 1) % 3] = 1
for i in range(1, n):
dp[i][rec[s[i]]] = min(
dp[i - 1][(rec[s[i]] - 1) % 3], dp[i - 1][(rec[s[i]] + 1) % 3]
)
dp[i][(rec[s[i]] - 1) % 3] = 1 + min(
dp[i - 1][(rec[s[i]] - 1 - 1) % 3], dp[i - 1][rec[s[i]] % 3]
)
dp[i][(rec[s[i]] + 1) % 3] = 1 + min(
dp[i - 1][rec[s[i]] % 3], dp[i - 1][(rec[s[i]] + 2) % 3]
)
result = ["" for i in range(n)]
if dp[n - 1][0] == min(dp[n - 1]):
temp = 0
elif dp[n - 1][1] == min(dp[n - 1]):
temp = 1
else:
temp = 2
result[n - 1] = recInv[temp]
for i in range(n - 2, -1, -1):
if dp[i][(temp - 1) % 3] == min(dp[i][(temp - 1) % 3], dp[i][(temp + 1) % 3]):
temp = (temp - 1) % 3
else:
temp = (temp + 1) % 3
result[i] = recInv[temp]
print(min(dp[n - 1]))
print("".join(result)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER STRING STRING STRING ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
s = input()
ss = s[0]
def ans(a, b):
if a == "R":
if b == "G":
return "B"
else:
return "G"
elif a == "G":
if b == "R":
return "B"
else:
return "R"
elif b == "R":
return "G"
else:
return "R"
m = 0
for i in range(1, n - 1):
if s[i] == ss[i - 1]:
ss = ss + ans(s[i], s[i + 1])
m += 1
else:
ss = ss + s[i]
if s[n - 1] == ss[len(ss) - 1]:
m += 1
ss = ss + ans(s[n - 1], s[n - 1])
else:
ss = ss + s[n - 1]
if n == 1:
print("0\n" + s)
else:
print(m)
print(ss) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR STRING IF VAR STRING RETURN STRING RETURN STRING IF VAR STRING IF VAR STRING RETURN STRING RETURN STRING IF VAR STRING RETURN STRING RETURN STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
s = input()
b = [0] * n
r = [0] * n
g = [0] * n
if s[0] == "R":
b[0] = 1
g[0] = 1
elif s[0] == "G":
b[0] = 1
r[0] = 1
else:
r[0] = 1
g[0] = 1
for i in range(1, n):
if s[i] == "R":
r[i] = min(0 + g[i - 1], 0 + b[i - 1])
g[i] = min(1 + r[i - 1], 1 + b[i - 1])
b[i] = min(1 + g[i - 1], 1 + r[i - 1])
elif s[i] == "G":
g[i] = min(0 + r[i - 1], 0 + b[i - 1])
r[i] = min(1 + g[i - 1], 1 + b[i - 1])
b[i] = min(1 + g[i - 1], 1 + r[i - 1])
else:
b[i] = min(0 + g[i - 1], 0 + r[i - 1])
r[i] = min(1 + g[i - 1], 1 + b[i - 1])
g[i] = min(1 + r[i - 1], 1 + b[i - 1])
print(min(r[-1], g[-1], b[-1]))
l = min(r[-1], g[-1], b[-1])
w = [""] * n
if l == b[-1]:
w[-1] = "B"
elif l == g[-1]:
w[-1] = "G"
else:
w[-1] = "R"
for i in range(n - 2, -1, -1):
if w[i + 1] == "B":
if r[i] <= g[i]:
w[i] = "R"
else:
w[i] = "G"
elif w[i + 1] == "G":
if r[i] <= b[i]:
w[i] = "R"
else:
w[i] = "B"
elif b[i] <= g[i]:
w[i] = "B"
else:
w[i] = "G"
print("".join(w)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER STRING IF VAR VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING IF VAR VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING IF VAR VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | input()
s = input()
def cost(p, s):
return sum(1 for i, x in enumerate(p) if len(s) - 1 >= i and x != s[i])
def solve(s):
ss = set(["R", "G", "B"])
ls = list(s)
cnt = 0
current = ls[0]
for i, x in enumerate(ls[1:], 1):
if current == x:
ns = set([ls[i], ls[i - 1]])
if i + 1 < len(s):
ns.add(ls[i + 1])
ls[i] = (ss - ns).pop()
cnt += 1
current = ls[i]
return cnt, "".join(ls)
ans = solve(s)
print(ans[0], ans[1]) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | def dif(n, m):
cnt = 0
for i, j in zip(n, m):
if j != i:
cnt += 1
return cnt
n = int(input())
inp = list(input())
if n == 1:
print(0)
print("".join(inp))
else:
s = inp[:]
cnt = 0
for i in range(1, n - 1):
if inp[i] == inp[i - 1]:
cnt += 1
ini = ["R", "G", "B"]
ini.remove(inp[i - 1])
if inp[i + 1] in ini:
ini.remove(inp[i + 1])
inp[i] = ini[0]
i += 1
inp.pop(-1)
if inp[-1] == s[-1]:
cnt += 1
if s[-1] == "R":
inp.append("B")
else:
inp.append("R")
else:
inp.append(s[-1])
print(dif(inp, s))
print("".join(inp)) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST STRING STRING STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
s = list(input())
ans = ""
ans += s[0]
count = 0
for i in range(1, n):
if s[i] == s[i - 1] == "R":
if i + 1 < n:
if s[i + 1] == "G":
s[i] = "B"
else:
s[i] = "G"
else:
s[i] = "B"
count += 1
elif s[i] == s[i - 1] == "B":
if i + 1 < n:
if s[i + 1] == "R":
s[i] = "G"
else:
s[i] = "R"
else:
s[i] = "R"
count += 1
elif s[i] == s[i - 1] == "G":
if i + 1 < n:
if s[i + 1] == "B":
s[i] = "R"
else:
s[i] = "B"
else:
s[i] = "B"
count += 1
print(count)
for i in range(len(s)):
print(s[i], end="") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | def main():
if input() == "1":
print(f"0\n{input()}")
return
a, b, *l = map({"R": 0, "G": 1, "B": 2}.get, input())
l.append((l[-1] + 1) % 3 if l else a)
res, cnt = ["RGB"[a]], 0
for c in l:
if b == a:
b = a - 2 if a == c else 3 - a - c
cnt += 1
res.append("RGB"[b])
a, b = b, c
print(cnt)
print("".join(res))
main() | FUNC_DEF IF FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_CALL VAR RETURN ASSIGN VAR VAR VAR FUNC_CALL VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR LIST STRING VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | a = int(input())
s = input()
ans = []
for i in range(len(s)):
ans.append(s[i])
count = 0
for i in range(1, len(s)):
if ans[i] != ans[i - 1]:
continue
if i == len(s) - 1:
if ans[i] == ans[i - 1]:
if ans[i - 1] == "R":
ans[i] = "G"
if ans[i - 1] == "G":
ans[i] = "R"
if ans[i - 1] == "B":
ans[i] = "R"
count += 1
continue
elif s[i - 1] == s[i + 1]:
if ans[i - 1] == "R":
ans[i] = "G"
if s[i - 1] == "G":
ans[i] = "R"
if s[i - 1] == "B":
ans[i] = "R"
count += 1
continue
else:
if (
ans[i - 1] == "R"
and ans[i + 1] == "G"
or ans[i - 1] == "G"
and ans[i + 1] == "R"
):
ans[i] = "B"
if (
ans[i - 1] == "B"
and ans[i + 1] == "G"
or ans[i - 1] == "G"
and ans[i + 1] == "B"
):
ans[i] = "R"
if (
ans[i - 1] == "R"
and ans[i + 1] == "B"
or ans[i - 1] == "B"
and ans[i + 1] == "R"
):
ans[i] = "G"
count += 1
continue
print(count)
print("".join(map(str, ans))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | input()
garor = list(input())
gar = garor.copy()
for i in range(1, len(gar)):
if gar[i] == gar[i - 1]:
options = ["R", "G", "B"]
options.remove(gar[i - 1])
if i < len(gar) - 1 and gar[i + 1] in options:
options.remove(gar[i + 1])
gar[i] = options[0]
count = 0
for i in range(0, len(gar)):
if gar[i] != garor[i]:
count = count + 1
print(count)
str1 = "".join(gar)
print(str1) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST STRING STRING STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
s = input()
s = list(s)
c = "RGB"
c = list(c)
count = 0
for i in range(1, len(s)):
c = "RGB"
c = list(c)
if s[i] == s[i - 1]:
count += 1
if i == n - 1:
c.remove(s[i - 1])
s[i] = c[0]
elif s[i + 1] != s[i - 1]:
c.remove(s[i - 1])
c.remove(s[i + 1])
s[i] = c[0]
else:
c.remove(s[i])
s[i] = c[0]
print(count)
for i in s:
print(i, end="") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' β colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of lamps.
The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' β colors of lamps in the garland.
-----Output-----
In the first line of the output print one integer $r$ β the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $t$ of length $n$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
-----Examples-----
Input
9
RBGRRBRGG
Output
2
RBGRGBRGR
Input
8
BBBGBRRR
Output
2
BRBGBRGR
Input
13
BBRRRRGGGGGRR
Output
6
BGRBRBGBGBGRG | n = int(input())
a = input()
z = [a[0]]
s = ["R", "B", "G"]
count = 0
for i in range(1, n):
if a[i] != z[-1]:
z.append(a[i])
else:
for j in s:
if i + 1 < n:
if j != a[i + 1] and j != a[i]:
z.append(j)
count += 1
break
elif j != a[i]:
z.append(j)
count += 1
break
print(count)
print("".join(z)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Recently you've discovered a new shooter. They say it has realistic game mechanics.
Your character has a gun with magazine size equal to $k$ and should exterminate $n$ waves of monsters. The $i$-th wave consists of $a_i$ monsters and happens from the $l_i$-th moment of time up to the $r_i$-th moments of time. All $a_i$ monsters spawn at moment $l_i$ and you have to exterminate all of them before the moment $r_i$ ends (you can kill monsters right at moment $r_i$). For every two consecutive waves, the second wave starts not earlier than the first wave ends (though the second wave can start at the same moment when the first wave ends) β formally, the condition $r_i \le l_{i + 1}$ holds. Take a look at the notes for the examples to understand the process better.
You are confident in yours and your character's skills so you can assume that aiming and shooting are instant and you need exactly one bullet to kill one monster. But reloading takes exactly $1$ unit of time.
One of the realistic mechanics is a mechanic of reloading: when you reload you throw away the old magazine with all remaining bullets in it. That's why constant reloads may cost you excessive amounts of spent bullets.
You've taken a liking to this mechanic so now you are wondering: what is the minimum possible number of bullets you need to spend (both used and thrown) to exterminate all waves.
Note that you don't throw the remaining bullets away after eradicating all monsters, and you start with a full magazine.
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \le n \le 2000$; $1 \le k \le 10^9$)Β β the number of waves and magazine size.
The next $n$ lines contain descriptions of waves. The $i$-th line contains three integers $l_i$, $r_i$ and $a_i$ ($1 \le l_i \le r_i \le 10^9$; $1 \le a_i \le 10^9$)Β β the period of time when the $i$-th wave happens and the number of monsters in it.
It's guaranteed that waves don't overlap (but may touch) and are given in the order they occur, i. e. $r_i \le l_{i + 1}$.
-----Output-----
If there is no way to clear all waves, print $-1$. Otherwise, print the minimum possible number of bullets you need to spend (both used and thrown) to clear all waves.
-----Examples-----
Input
2 3
2 3 6
3 4 3
Output
9
Input
2 5
3 7 11
10 12 15
Output
30
Input
5 42
42 42 42
42 43 42
43 44 42
44 45 42
45 45 1
Output
-1
Input
1 10
100 111 1
Output
1
-----Note-----
In the first example: At the moment $2$, the first wave occurs and $6$ monsters spawn. You kill $3$ monsters and start reloading. At the moment $3$, the second wave occurs and $3$ more monsters spawn. You kill remaining $3$ monsters from the first wave and start reloading. At the moment $4$, you kill remaining $3$ monsters from the second wave. In total, you'll spend $9$ bullets.
In the second example: At moment $3$, the first wave occurs and $11$ monsters spawn. You kill $5$ monsters and start reloading. At moment $4$, you kill $5$ more monsters and start reloading. At moment $5$, you kill the last monster and start reloading throwing away old magazine with $4$ bullets. At moment $10$, the second wave occurs and $15$ monsters spawn. You kill $5$ monsters and start reloading. At moment $11$, you kill $5$ more monsters and start reloading. At moment $12$, you kill last $5$ monsters. In total, you'll spend $30$ bullets. | import sys
n, k = list(map(int, sys.stdin.readline().strip().split()))
L = []
R = []
A = []
for i in range(0, n):
x = list(map(int, sys.stdin.readline().strip().split()))
L.append(x[0])
R.append(x[1])
A.append(x[2])
L.append(R[-1])
i = n - 1
x = 0
y = 0
ans = 0
v = True
N = [(0) for i in range(0, n)]
while i >= 0:
if R[i] == L[i + 1]:
x = max(x + A[i] - k * (R[i] - L[i]), 0)
N[i] = x
else:
x = max(A[i] - k * (R[i] - L[i]), 0)
N[i] = x
if N[i] > k:
v = False
i = i - 1
m = k
N.append(0)
i = 0
while i < n and v == True:
if m < N[i]:
ans = ans + m
m = k
m = m - A[i]
ans = ans + A[i]
while m < 0:
m = m + k
i = i + 1
if v == True:
print(ans)
else:
print(-1) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Recently you've discovered a new shooter. They say it has realistic game mechanics.
Your character has a gun with magazine size equal to $k$ and should exterminate $n$ waves of monsters. The $i$-th wave consists of $a_i$ monsters and happens from the $l_i$-th moment of time up to the $r_i$-th moments of time. All $a_i$ monsters spawn at moment $l_i$ and you have to exterminate all of them before the moment $r_i$ ends (you can kill monsters right at moment $r_i$). For every two consecutive waves, the second wave starts not earlier than the first wave ends (though the second wave can start at the same moment when the first wave ends) β formally, the condition $r_i \le l_{i + 1}$ holds. Take a look at the notes for the examples to understand the process better.
You are confident in yours and your character's skills so you can assume that aiming and shooting are instant and you need exactly one bullet to kill one monster. But reloading takes exactly $1$ unit of time.
One of the realistic mechanics is a mechanic of reloading: when you reload you throw away the old magazine with all remaining bullets in it. That's why constant reloads may cost you excessive amounts of spent bullets.
You've taken a liking to this mechanic so now you are wondering: what is the minimum possible number of bullets you need to spend (both used and thrown) to exterminate all waves.
Note that you don't throw the remaining bullets away after eradicating all monsters, and you start with a full magazine.
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \le n \le 2000$; $1 \le k \le 10^9$)Β β the number of waves and magazine size.
The next $n$ lines contain descriptions of waves. The $i$-th line contains three integers $l_i$, $r_i$ and $a_i$ ($1 \le l_i \le r_i \le 10^9$; $1 \le a_i \le 10^9$)Β β the period of time when the $i$-th wave happens and the number of monsters in it.
It's guaranteed that waves don't overlap (but may touch) and are given in the order they occur, i. e. $r_i \le l_{i + 1}$.
-----Output-----
If there is no way to clear all waves, print $-1$. Otherwise, print the minimum possible number of bullets you need to spend (both used and thrown) to clear all waves.
-----Examples-----
Input
2 3
2 3 6
3 4 3
Output
9
Input
2 5
3 7 11
10 12 15
Output
30
Input
5 42
42 42 42
42 43 42
43 44 42
44 45 42
45 45 1
Output
-1
Input
1 10
100 111 1
Output
1
-----Note-----
In the first example: At the moment $2$, the first wave occurs and $6$ monsters spawn. You kill $3$ monsters and start reloading. At the moment $3$, the second wave occurs and $3$ more monsters spawn. You kill remaining $3$ monsters from the first wave and start reloading. At the moment $4$, you kill remaining $3$ monsters from the second wave. In total, you'll spend $9$ bullets.
In the second example: At moment $3$, the first wave occurs and $11$ monsters spawn. You kill $5$ monsters and start reloading. At moment $4$, you kill $5$ more monsters and start reloading. At moment $5$, you kill the last monster and start reloading throwing away old magazine with $4$ bullets. At moment $10$, the second wave occurs and $15$ monsters spawn. You kill $5$ monsters and start reloading. At moment $11$, you kill $5$ more monsters and start reloading. At moment $12$, you kill last $5$ monsters. In total, you'll spend $30$ bullets. | import sys
n, k = map(int, input().split())
L, R, A = [0] * n, [0] * n, [0] * n
for i in range(n):
L[i], R[i], A[i] = map(int, input().split())
dp = [0] * (n + 1)
for i in range(n - 1, -1, -1):
need = A[i]
if i < n - 1 and R[i] == L[i + 1]:
need += dp[i + 1]
if (R[i] - L[i] + 1) * k < need:
print(-1)
sys.exit()
dp[i] = max(0, need - (R[i] - L[i]) * k)
rem, ans = k, 0
for i in range(n):
if rem < dp[i]:
ans += rem
rem = k
ans += A[i]
rem = (rem - A[i]) % k
print(ans) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Recently you've discovered a new shooter. They say it has realistic game mechanics.
Your character has a gun with magazine size equal to $k$ and should exterminate $n$ waves of monsters. The $i$-th wave consists of $a_i$ monsters and happens from the $l_i$-th moment of time up to the $r_i$-th moments of time. All $a_i$ monsters spawn at moment $l_i$ and you have to exterminate all of them before the moment $r_i$ ends (you can kill monsters right at moment $r_i$). For every two consecutive waves, the second wave starts not earlier than the first wave ends (though the second wave can start at the same moment when the first wave ends) β formally, the condition $r_i \le l_{i + 1}$ holds. Take a look at the notes for the examples to understand the process better.
You are confident in yours and your character's skills so you can assume that aiming and shooting are instant and you need exactly one bullet to kill one monster. But reloading takes exactly $1$ unit of time.
One of the realistic mechanics is a mechanic of reloading: when you reload you throw away the old magazine with all remaining bullets in it. That's why constant reloads may cost you excessive amounts of spent bullets.
You've taken a liking to this mechanic so now you are wondering: what is the minimum possible number of bullets you need to spend (both used and thrown) to exterminate all waves.
Note that you don't throw the remaining bullets away after eradicating all monsters, and you start with a full magazine.
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \le n \le 2000$; $1 \le k \le 10^9$)Β β the number of waves and magazine size.
The next $n$ lines contain descriptions of waves. The $i$-th line contains three integers $l_i$, $r_i$ and $a_i$ ($1 \le l_i \le r_i \le 10^9$; $1 \le a_i \le 10^9$)Β β the period of time when the $i$-th wave happens and the number of monsters in it.
It's guaranteed that waves don't overlap (but may touch) and are given in the order they occur, i. e. $r_i \le l_{i + 1}$.
-----Output-----
If there is no way to clear all waves, print $-1$. Otherwise, print the minimum possible number of bullets you need to spend (both used and thrown) to clear all waves.
-----Examples-----
Input
2 3
2 3 6
3 4 3
Output
9
Input
2 5
3 7 11
10 12 15
Output
30
Input
5 42
42 42 42
42 43 42
43 44 42
44 45 42
45 45 1
Output
-1
Input
1 10
100 111 1
Output
1
-----Note-----
In the first example: At the moment $2$, the first wave occurs and $6$ monsters spawn. You kill $3$ monsters and start reloading. At the moment $3$, the second wave occurs and $3$ more monsters spawn. You kill remaining $3$ monsters from the first wave and start reloading. At the moment $4$, you kill remaining $3$ monsters from the second wave. In total, you'll spend $9$ bullets.
In the second example: At moment $3$, the first wave occurs and $11$ monsters spawn. You kill $5$ monsters and start reloading. At moment $4$, you kill $5$ more monsters and start reloading. At moment $5$, you kill the last monster and start reloading throwing away old magazine with $4$ bullets. At moment $10$, the second wave occurs and $15$ monsters spawn. You kill $5$ monsters and start reloading. At moment $11$, you kill $5$ more monsters and start reloading. At moment $12$, you kill last $5$ monsters. In total, you'll spend $30$ bullets. | import sys
input = sys.stdin.buffer.readline
n, k = map(int, input().split())
tank = [(k, 0)]
new = []
L = [0] * n
R = [0] * n
A = [0] * n
for i in range(n):
L[i], R[i], A[i] = map(int, input().split())
for i in range(n):
new = []
l, r, a = L[i], R[i], A[i]
val = -1
for p, q in tank:
if p + (r - l) * k < a:
continue
ct, v = 0, 0
if p >= a:
new.append((p - a, q + a))
v = p - a
else:
tmp = a - p
if tmp % k == 0:
new.append((0, q + a))
ct = tmp // k
else:
amari = (tmp // k + 1) * k
new.append((p + amari - a, q + a))
ct = tmp // k + 1
v = p + amari - a
if r - l > ct or i < n - 1 and r < L[i + 1]:
if val == -1 or val > q + a + v:
val = q + a + v
if val != -1:
new.append((k, val))
tank = new
res = -1
for p, q in tank:
if res == -1:
res = q
elif res > q:
res = q
print(res) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
dp = [([-1] * (N + 1)) for _ in range(N + 1)]
for l in range(N):
dp[l][l + 1] = A[l]
for d in range(2, N + 1):
for l in range(N - d + 1):
for t in range(1, d):
if dp[l][l + t] == dp[l + t][l + d] and dp[l][l + t] != -1:
dp[l][l + d] = dp[l][l + t] + 1
break
dp2 = [i for i in range(N + 1)]
for r in range(1, N + 1):
if dp[0][r] != -1:
dp2[r] = 1
for l in range(N):
for r in range(l + 2, N + 1):
if dp[l + 1][r] != -1:
dp2[r] = min(dp2[l + 1] + 1, dp2[r])
else:
dp2[r] = min(dp2[l + 1] + (r - l - 1), dp2[r])
print(dp2[N]) | 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | n = int(input())
arr = list(map(int, input().split()))
tracker = [([-1] * (n + 1)) for _ in range(2024)]
d = [[] for _ in range(n)]
for j, v in enumerate(arr):
tracker[v][j] = j
d[j].append(j)
for v in range(1, 2024):
for i in range(n):
j = tracker[v][i]
h = tracker[v][j + 1] if j != -1 else -1
if j != -1 and h != -1:
tracker[v + 1][i] = h
d[i].append(h)
a = [_ for _ in range(1, n + 1)]
for s in range(n):
for tracker in d[s]:
a[tracker] = min(a[tracker], a[s - 1] + 1 if s > 0 else 1)
print(a[n - 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 NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
DP = [([-1] * (n + 1)) for i in range(n + 1)]
for i in range(n):
DP[i][i] = A[i]
for mid in range(1, n):
for i in range(n):
j = i + mid
if j == n:
break
for k in range(i, j + 1):
if DP[i][k] == DP[k + 1][j] and DP[i][k] != -1:
DP[i][j] = DP[i][k] + 1
ANS = [2000] * (n + 1)
ANS.append(0)
for i in range(n):
ANS[i] = min(ANS[i], ANS[i - 1] + 1)
for j in range(i, n):
if DP[i][j] != -1:
ANS[j] = min(ANS[j], ANS[i - 1] + 1)
print(ANS[n - 1]) | 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | n = int(input())
a = list(map(int, input().split(" ")))
new_a = [([0] * 600) for i in range(600)]
dp = [([2147483647] * 600) for i in range(600)]
for i in range(n):
new_a[i + 1][i + 1] = a[i]
dp[i + 1][i + 1] = 1
for i in range(1, n + 1):
for j in range(i + 1, n + 1):
dp[i][j] = j - i + 1
for llen in range(2, n + 1):
for left in range(1, n - llen + 2):
right = left + llen - 1
for middle in range(left, right):
dp[left][right] = min(
dp[left][right], dp[left][middle] + dp[middle + 1][right]
)
if (
dp[left][middle] == 1
and dp[middle + 1][right] == 1
and new_a[left][middle] == new_a[middle + 1][right]
):
dp[left][right] = 1
new_a[left][right] = new_a[left][middle] + 1
print(dp[1][n]) | 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 BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | n = int(input())
b = [int(_) for _ in input().split()]
d = [[(b[i] if i == j else -1) for i in range(n)] for j in range(n)]
def f(i, j):
if d[i][j] != -1:
return d[i][j]
d[i][j] = 0
for m in range(i, j):
l = f(i, m)
if f(m + 1, j) == l and l:
d[i][j] = l + 1
break
return d[i][j]
a = [_ for _ in range(1, n + 1)]
for e in range(1, n):
for s in range(e + 1):
if f(s, e):
a[e] = min(a[e], a[s - 1] + 1 if s > 0 else a[s])
print(a[-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | printn = lambda x: print(x, end="")
inn = lambda: int(input())
inl = lambda: list(map(int, input().split()))
inm = lambda: map(int, input().split())
ins = lambda: input().strip()
DBG = True and False
BIG = 10**18
R = 10**9 + 7
def ddprint(x):
if DBG:
print(x)
def dp(l, r):
if dpa[l][r] >= 0:
return dpa[l][r]
elif l == r:
dpa[l][r] = a[l]
else:
dpa[l][r] = 0
for j in range(l, r):
x = dp(l, j)
y = dp(j + 1, r)
if 0 < x == y:
dpa[l][r] = x + 1
return dpa[l][r]
n = inn()
a = inl()
dpa = [([-1] * (n + 1)) for i in range(n + 1)]
dp2 = [BIG] * (n + 1)
dp2[0] = 1
for i in range(n):
if dp(0, i) > 0:
dp2[i] = 1
else:
mn = i + 1
for j in range(i):
x = dp(j + 1, i)
if 0 < x:
mn = min(mn, dp2[j] + 1)
dp2[i] = mn
for i in range(n):
ddprint(dpa[i])
ddprint(dp2)
print(dp2[n - 1]) | ASSIGN VAR FUNC_CALL VAR VAR STRING 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | import sys
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 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():
inf = 10**9
n = II()
aa = LI()
dp1 = [([-1] * n) for _ in range(n)]
dp2 = [([inf] * n) for _ in range(n)]
for i in range(n):
dp1[i][i] = aa[i]
dp2[i][i] = 1
for w in range(2, n + 1):
for l in range(n - w + 1):
r = l + w - 1
for m in range(l, r):
if dp1[l][m] != -1 and dp1[l][m] == dp1[m + 1][r]:
dp1[l][r] = dp1[l][m] + 1
dp2[l][r] = 1
for m in range(n):
for l in range(m + 1):
for r in range(m + 1, n):
dp2[l][r] = min(dp2[l][r], dp2[l][m] + dp2[m + 1][r])
print(dp2[0][n - 1])
main() | IMPORT 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 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 ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | import sys
dp = []
a = []
def calcdp(l, r):
global dp, a
if l + 1 == r:
dp[l][r] = a[l]
return dp[l][r]
if dp[l][r] != 0:
return dp[l][r]
dp[l][r] = -1
for k in range(l + 1, r):
la = calcdp(l, k)
ra = calcdp(k, r)
if la > 0 and la == ra:
dp[l][r] = la + 1
return dp[l][r]
def solve(n):
dp2 = [float("inf")] * (n + 1)
dp2[0] = 0
for i in range(n):
for j in range(i + 1, n + 1):
if calcdp(i, j) > 0:
dp2[j] = min(dp2[j], dp2[i] + 1)
return dp2[n]
def ip():
global dp, a
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
a.append(0)
dp = []
ll = [0] * (n + 1)
for _ in range(n + 1):
dp.append(list(ll))
print(solve(n))
ip() | IMPORT ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN 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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | n = int(input())
a = list(map(int, input().split()))
dp = [([505] * n) for _ in range(n)]
Max = [([0] * n) for _ in range(n)]
for i in range(n):
dp[i][i] = 1
Max[i][i] = a[i]
for len in range(1, n + 1):
for i in range(n - len + 1):
j = i + len - 1
for k in range(i, j):
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j])
if dp[i][k] == 1 and dp[k + 1][j] == 1 and Max[i][k] == Max[k + 1][j]:
dp[i][j] = 1
Max[i][j] = Max[i][k] + 1
print(dp[0][n - 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 NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | n = int(input())
a = list(map(int, input().split()))
dp = [([False] * (n + 1)) for i in range(n + 1)]
def solve(l, r):
if dp[l][r]:
return dp[l][r]
if r - l == 1:
dp[l][r] = a[l], 1
return dp[l][r]
tmp = 10**9
for i in range(l + 1, r):
if solve(l, i)[0] == -1 or solve(i, r)[0] == -1:
tmp = min(tmp, dp[l][i][1] + dp[i][r][1])
elif solve(l, i) == solve(i, r):
tmp = solve(l, i)[0] + 1
dp[l][r] = tmp, 1
return dp[l][r]
else:
tmp = min(tmp, 2)
dp[l][r] = -1, tmp
return dp[l][r]
solve(0, n)
print(dp[0][n][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 NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | def f():
n = int(input())
A = [int(s) for s in input().split()]
memo = [[None for j in range(n + 1)] for i in range(n + 1)]
for i in range(n):
memo[i][i] = [A[i], A[i], 1]
for l in range(2, n + 1):
for left in range(0, n - l + 1):
right = left + l - 1
minLen = l
shortestMid = right
for mid in range(left + 1, right + 1):
pre = memo[left][mid - 1]
post = memo[mid][right]
combLen = pre[2] + post[2]
if pre[1] == post[0]:
combLen -= 1
if combLen < minLen:
minLen = combLen
shortestMid = mid
pre = memo[left][shortestMid - 1]
post = memo[shortestMid][right]
startEle = pre[0]
endEle = post[1]
if pre[2] == 1:
if pre[0] == post[0]:
startEle = pre[0] + 1
if post[2] == 1:
if pre[1] == post[0]:
endEle = post[0] + 1
memo[left][right] = [startEle, endEle, minLen]
print(memo[0][n - 1][2])
f() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | import sys
readline = sys.stdin.buffer.readline
N = int(readline())
A = list(map(int, readline().split()))
dp = [([0] * N) for _ in range(N)]
for j in range(N):
dp[j][0] = A[j]
for l in range(1, N):
for j in range(l, N):
for k in range(j - l, j):
if dp[k][k - j + l] == dp[j][j - k - 1] > 0:
dp[j][l] = 1 + dp[j][j - k - 1]
break
dp = [None] + dp
Dp = [0] * (N + 1)
for j in range(1, N + 1):
res = N
for l in range(j):
if dp[j][l]:
res = min(res, 1 + Dp[j - l - 1])
Dp[j] = res
print(Dp[N]) | 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 BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | from sys import gettrace, stdin
if not gettrace():
def input():
return next(stdin)[:-1]
INF = 10000
def main():
n = int(input())
aa = [int(a) for a in input().split()]
dp = [([0] * (n + 1)) for _ in range(n)]
def calc_dp(i, j):
if i + 1 == j:
dp[i][j] = aa[i]
if dp[i][j] != 0:
return dp[i][j]
dp[i][j] = -1
for k in range(i + 1, j):
lf = calc_dp(i, k)
rg = calc_dp(k, j)
if lf > 0 and lf == rg:
dp[i][j] = lf + 1
break
return dp[i][j]
dp2 = list(range(0, n + 1))
for i in range(n):
for j in range(i + 1, n + 1):
if calc_dp(i, j) > 0:
dp2[j] = min(dp2[j], dp2[i] + 1)
print(dp2[n])
main() | IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF 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 BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | import sys
from sys import stdin, stdout
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().strip().split(" ")))
dp_arr = [[None for i in range(n)] for i in range(n)]
for i in range(n):
dp_arr[i][i] = arr[i], 1, arr[i]
def merge_small(c1, c2):
if c1[1] == 1 and c2[1] == 1:
if c1[0] == c2[0]:
return c1[0] + 1, 1, c1[0] + 1
else:
return c1[0], 2, c2[0]
elif c1[1] == 2 and c2[1] == 1:
if c1[2] == c2[0]:
if c1[0] == c1[2] + 1:
return c1[0] + 1, 1, c1[0] + 1
else:
return c1[0], 2, c2[2] + 1
else:
return c1[0], 3, c2[2]
elif c1[1] == 1 and c2[1] == 2:
if c1[2] == c2[0]:
if c2[2] == c2[0] + 1:
return c2[2] + 1, 1, c2[2] + 1
else:
return c2[0] + 1, 2, c2[2]
else:
return c1[0], 3, c2[2]
elif c1[1] == 2 and c2[1] == 2:
if c1[2] == c2[0]:
c1 = c1[0], 2, c1[2] + 1
c2 = c2[2], 1, c2[2]
if c1[1] == 2 and c2[1] == 1:
if c1[2] == c2[0]:
if c1[0] == c1[2] + 1:
return c1[0] + 1, 1, c1[0] + 1
else:
return c1[0], 2, c2[2] + 1
else:
return c1[0], 3, c2[2]
else:
return c1[0], 4, c2[2]
def merge_main(c1, c2):
if c1[1] > 2:
if c2[1] > 2:
if c1[2] == c2[0]:
return c1[0], c1[1] + c2[1] - 1, c2[2]
else:
return c1[0], c1[1] + c2[1], c2[2]
else:
if c2[1] == 1:
if c1[2] == c2[0]:
return c1[0], c1[1], c2[2] + 1
else:
return c1[0], c1[1] + 1, c2[2]
if c2[1] == 2:
if c1[2] == c2[0]:
if c1[2] + 1 == c2[2]:
return c1[0], c1[1], c2[2] + 1
else:
return c1[0], c1[1] + 1, c2[2]
else:
return c1[0], c1[1] + 2, c2[2]
elif c2[1] > 2:
if c1[1] == 1:
if c1[2] == c2[0]:
return c1[2] + 1, c2[1], c2[2]
else:
return c1[2], c2[1] + 1, c2[2]
if c1[1] == 2:
if c1[2] == c2[0]:
if c1[0] == c1[2] + 1:
return c1[0] + 1, c2[1], c2[2]
else:
return c1[0], c2[1] + 1, c2[2]
else:
return c1[0], c2[1] + 2, c2[2]
else:
return merge_small(c1, c2)
for i1 in range(1, n):
for j1 in range(n - i1):
curr_pos = j1, j1 + i1
for k1 in range(j1, j1 + i1):
res = merge_main(dp_arr[j1][k1], dp_arr[k1 + 1][j1 + i1])
if dp_arr[j1][j1 + i1] == None or dp_arr[j1][j1 + i1][1] > res[1]:
dp_arr[j1][j1 + i1] = res
stdout.write(str(dp_arr[0][n - 1][1]) + "\n") | IMPORT 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 ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR NONE VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER STRING |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | n = int(input())
b = [int(_) for _ in input().split()]
d = [[(-1 if i != j else b[i]) for i in range(n)] for j in range(n)]
for l in range(1, n):
for s in range(n - l):
e = s + l
for m in range(s, e):
if d[s][m] == d[m + 1][e] and d[s][m] != -1:
d[s][e] = d[s][m] + 1
a = [1]
for e in range(1, n):
t = 4096
for s in range(e + 1):
if d[s][e] != -1:
t = min(t, a[s - 1] + 1 if s > 0 else a[s])
a.append(t)
print(a[-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | from sys import stdin, stdout
def dfs(l, r, dp, a_a):
if l == r:
return a_a[l]
if l + 1 == r:
if a_a[l] == a_a[r]:
return a_a[l] + 1
else:
return -1
if dp[l][r] != 10**6:
return dp[l][r]
dp[l][r] = -1
for m in range(l, r):
r1 = dfs(l, m, dp, a_a)
r2 = dfs(m + 1, r, dp, a_a)
if r1 > 0 and r1 == r2:
dp[l][r] = r1 + 1
return dp[l][r]
return dp[l][r]
def array_shrinking(n, a_a):
dp = [[(10**6) for _ in range(n)] for _ in range(n)]
dp2 = [(10**6) for _ in range(n)]
for i in range(n):
dp2[i] = min(i + 1, dp2[i])
for j in range(i, n):
r = dfs(i, j, dp, a_a)
if r != -1:
if i > 0:
dp2[j] = min(dp2[i - 1] + 1, dp2[j])
else:
dp2[j] = min(1, dp2[j])
return dp2[n - 1]
n = int(stdin.readline())
a_a = list(map(int, stdin.readline().split()))
res = array_shrinking(n, a_a)
stdout.write(str(res)) | FUNC_DEF IF VAR VAR RETURN VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR RETURN BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP NUMBER NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | import sys
input = sys.stdin.readline
n = int(input().strip())
a = [int(x) for x in input().strip().split()]
dp = [([0] * n) for i in range(n)]
for i in range(n):
dp[i][i] = [a[i], 1]
for i in range(1, n):
for j in range(n - i):
v, c = -1, i + 1
for k in range(i):
if dp[j][j + k][0] != -1 and dp[j][j + k][0] == dp[j + k + 1][j + i][0]:
v, c = dp[j][j + k][0] + 1, 1
break
else:
v, c = -1, min(c, dp[j][j + k][1] + dp[j + k + 1][j + i][1])
dp[j][j + i] = [v, c]
print(dp[0][-1][1]) | IMPORT ASSIGN 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 ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | import sys
pl = 1
if pl:
input = sys.stdin.readline
else:
sys.stdin = open("input.txt", "r")
sys.stdout = open("outpt.txt", "w")
def li():
return [int(xxx) for xxx in input().split()]
def fi():
return int(input())
def si():
return list(input().rstrip())
def mi():
return map(int, input().split())
t = 1
while t > 0:
t -= 1
n = fi()
a = li()
dp = [([0] * (n + 1)) for i in range(n + 1)]
for i in range(n - 1, -1, -1):
for j in range(i, n):
if i == j:
dp[i][j] = a[i]
elif i == j - 1:
if a[i] == a[j]:
dp[i][j] = a[i] + 1
else:
for k in range(i, j):
if dp[i][k] and dp[k + 1][j] and dp[i][k] == dp[k + 1][j]:
dp[i][j] = dp[i][k] + 1
break
ans = [10**18] * (n + 1)
ans[-1] = 0
for i in range(n - 1, -1, -1):
for j in range(i, n):
if dp[i][j]:
ans[i] = min(ans[i], 1 + ans[j + 1])
else:
ans[i] = min(ans[i], j - i + 1 + ans[j + 1])
print(ans[0]) | IMPORT ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
dp = [([1000] * (n + 1)) for i in range(n + 1)]
val = [([0] * (n + 1)) for i in range(n + 1)]
for i in range(n):
dp[i][i + 1] = 1
val[i][i + 1] = a[i]
for p in range(2, n + 1):
for i in range(n - p + 1):
j = i + p
for k in range(i + 1, j):
if dp[i][k] == dp[k][j] == 1 and val[i][k] == val[k][j]:
dp[i][j] = 1
val[i][j] = val[i][k] + 1
else:
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
print(dp[0][n]) | 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | N = int(input())
L = list(map(int, input().split()))
DP = [([-1] * N) for i in range(N)]
for d in range(N):
for s in range(N - d):
e = s + d
if s == e:
DP[s][e] = L[s]
continue
for m in range(s, e):
l = DP[s][m]
r = DP[m + 1][e]
if l == r and l != -1:
DP[s][e] = max(DP[s][e], l + 1)
DP2 = [(i + 1) for i in range(N)]
for i in range(N):
if DP[0][i] != -1:
DP2[i] = 1
continue
for j in range(i):
if DP[j + 1][i] != -1:
DP2[i] = min(DP2[i], DP2[j] + 1)
print(DP2[N - 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 NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | n = int(input())
a = list(map(int, input().split()))
grip = [([-1] * (n - i)) for i in range(n)]
grip[0] = a.copy()
for level in range(1, n):
for left in range(n - level):
for split in range(level):
pl = grip[level - split - 1][left]
pr = grip[split][left + level - split]
if pl == pr != -1:
grip[level][left] = pl + 1
pref = [0] * (n + 1)
for p in range(1, n + 1):
x = n
for j in range(p):
l = pref[j]
r = grip[p - j - 1][j]
if r == -1:
r = p - j
else:
r = 1
x = min(x, l + r)
pref[p] = x
print(pref[-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 NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | N = int(input())
arr = list(map(int, input().split()))
dp = [[(-1) for x in range(N)] for y in range(N)]
for size in range(1, N + 1):
for i in range(N - size + 1):
j = i + size - 1
if i == j:
dp[i][j] = arr[i]
else:
for k in range(i, j):
if dp[i][k] != -1 and dp[i][k] == dp[k + 1][j]:
dp[i][j] = dp[i][k] + 1
dp2 = [(x + 1) for x in range(N)]
for i in range(N):
for k in range(i + 1):
if dp[k][i] != -1:
if k == 0:
dp2[i] = 1
else:
dp2[i] = min(dp2[i], dp2[k - 1] + 1)
print(dp2[N - 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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | n = int(input())
li = list(map(int, input().split(" ")))
dp1 = []
for i in range(n):
lis = [-1] * n
dp1.append(lis)
dp2 = [0] * n
for i in range(n):
dp1[i][i] = li[i]
for i in range(n):
dp2[i] = i + 1
size = 2
while size <= n:
i = 0
while i < n - size + 1:
j = i + size - 1
k = i
while k < j:
if dp1[i][k] != -1:
if dp1[i][k] == dp1[k + 1][j]:
dp1[i][j] = dp1[i][k] + 1
k += 1
i += 1
size += 1
i = 0
while i < n:
k = 0
while k <= i:
if dp1[k][i] != -1:
if k == 0:
dp2[i] = 1
else:
dp2[i] = min(dp2[i], dp2[k - 1] + 1)
k += 1
i += 1
print(dp2[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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get?
-----Input-----
The first line contains the single integer $n$ ($1 \le n \le 500$) β the initial length of the array $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β the initial array $a$.
-----Output-----
Print the only integer β the minimum possible length you can get after performing the operation described above any number of times.
-----Examples-----
Input
5
4 3 2 2 3
Output
2
Input
7
3 3 4 4 4 3 3
Output
2
Input
3
1 3 5
Output
3
Input
1
1000
Output
1
-----Note-----
In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$.
In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$.
In the third and fourth tests, you can't perform the operation at all. | n = int(input())
a = [int(x) for x in input().split()]
dp = [([-1] * (n + 1)) for _ in range(n + 1)]
for i in range(n):
dp[i][i + 1] = a[i]
for leng in range(2, n + 1):
for l in range(n + 1):
if l + leng > n:
continue
r = l + leng
for mid in range(l + 1, n + 1):
if dp[l][mid] != -1 and dp[l][mid] == dp[mid][r]:
dp[l][r] = dp[l][mid] + 1
dp2 = [float("inf") for _ in range(n + 1)]
for i in range(n + 1):
dp2[i] = i
for j in range(i):
if dp[j][i] != -1:
dp2[i] = min(dp2[i], dp2[j] + 1)
print(dp2[n]) | 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 BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Read problems statements in Russian also.
Chef plays with the sequence of N numbers. During a single move Chef is able to choose a non-decreasing subsequence of the sequence and to remove it from the sequence. Help him to remove all the numbers in the minimal number of moves.
------ Input ------
The first line of each test case contains a single N denoting the number of integers in the given sequence. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N} denoting the given sequence
------ Output ------
Output a single line containing the minimal number of moves required to remove all the numbers from the sequence.
------ Constraints ------
$1 β€ N β€ 100000.$
$1 β€ A_{i} β€ 100000.$
Β
------ Scoring ------
Subtask 1 (10 points): N = 10
Subtask 2 (40 points): N = 2000
Subtask 2 (50 points): N = 100000
----- Sample Input 1 ------
3
1 2 3
----- Sample Output 1 ------
1
----- explanation 1 ------
----- Sample Input 2 ------
4
4 1 2 3
----- Sample Output 2 ------
2
----- explanation 2 ------ | def CeilIndex(A, l, r, key):
while r - l > 1:
m = l + (r - l) // 2
if A[m] >= key:
r = m
else:
l = m
return r
def lds(A, size):
tailTable = [(0) for i in range(size + 1)]
len = 0
tailTable[0] = A[0]
len = 1
for i in range(1, size):
if A[i] < tailTable[0]:
tailTable[0] = A[i]
elif A[i] > tailTable[len - 1]:
tailTable[len] = A[i]
len += 1
else:
tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]
return len
t = 1
while t > 0:
t -= 1
n = int(input())
arr = list(map(int, input().strip().split()))[:n]
for i in range(n):
arr[i] *= -1
print(lds(arr, n)) | FUNC_DEF WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER 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 FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Read problems statements in Russian also.
Chef plays with the sequence of N numbers. During a single move Chef is able to choose a non-decreasing subsequence of the sequence and to remove it from the sequence. Help him to remove all the numbers in the minimal number of moves.
------ Input ------
The first line of each test case contains a single N denoting the number of integers in the given sequence. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N} denoting the given sequence
------ Output ------
Output a single line containing the minimal number of moves required to remove all the numbers from the sequence.
------ Constraints ------
$1 β€ N β€ 100000.$
$1 β€ A_{i} β€ 100000.$
Β
------ Scoring ------
Subtask 1 (10 points): N = 10
Subtask 2 (40 points): N = 2000
Subtask 2 (50 points): N = 100000
----- Sample Input 1 ------
3
1 2 3
----- Sample Output 1 ------
1
----- explanation 1 ------
----- Sample Input 2 ------
4
4 1 2 3
----- Sample Output 2 ------
2
----- explanation 2 ------ | n = int(input())
arr = list(map(int, input().split()))
lds = []
for i in arr:
lo, hi = 0, len(lds)
while lo < hi:
mid = lo + hi >> 1
if i < lds[mid]:
lo = mid + 1
else:
hi = mid
if lo == len(lds):
lds.append(i)
else:
lds[lo] = i
print(len(lds)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with $2$ rows and $n$ columns, every element of which is either $0$ or $1$. In one move you can swap two values in neighboring cells.
More formally, let's number rows $1$ to $2$ from top to bottom, and columns $1$ to $n$ from left to right. Also, let's denote a cell in row $x$ and column $y$ as $(x, y)$. We consider cells $(x_1, y_1)$ and $(x_2, y_2)$ neighboring if $|x_1 - x_2| + |y_1 - y_2| = 1$.
Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.
-----Input-----
The first line contains an integer $n$ ($1 \leq n \leq 200000$) β the number of columns in the puzzle.
Following two lines describe the current arrangement on the puzzle. Each line contains $n$ integers, every one of which is either $0$ or $1$.
The last two lines describe Alice's desired arrangement in the same format.
-----Output-----
If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print $-1$.
-----Examples-----
Input
5
0 1 0 1 0
1 1 0 0 1
1 0 1 0 1
0 0 1 1 0
Output
5
Input
3
1 0 0
0 0 0
0 0 0
0 0 0
Output
-1
-----Note-----
In the first example the following sequence of swaps will suffice:
$(2, 1), (1, 1)$,
$(1, 2), (1, 3)$,
$(2, 2), (2, 3)$,
$(1, 4), (1, 5)$,
$(2, 5), (2, 4)$.
It can be shown that $5$ is the minimal possible answer in this case.
In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is $-1$. | n = int(input())
a = [list(map(int, input().split())), list(map(int, input().split()))]
b = [list(map(int, input().split())), list(map(int, input().split()))]
s1 = 0
s2 = 0
stp = 0
for i in range(n):
s1 += a[0][i] - b[0][i]
s2 += a[1][i] - b[1][i]
if s1 * s2 < 0:
k = min(abs(s1), abs(s2))
if s1 < 0:
s1 += k
s2 -= k
else:
s2 += k
s1 -= k
stp += k
stp += abs(s1) + abs(s2)
if s1 + s2 != 0:
print(-1)
else:
print(stp) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with $2$ rows and $n$ columns, every element of which is either $0$ or $1$. In one move you can swap two values in neighboring cells.
More formally, let's number rows $1$ to $2$ from top to bottom, and columns $1$ to $n$ from left to right. Also, let's denote a cell in row $x$ and column $y$ as $(x, y)$. We consider cells $(x_1, y_1)$ and $(x_2, y_2)$ neighboring if $|x_1 - x_2| + |y_1 - y_2| = 1$.
Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.
-----Input-----
The first line contains an integer $n$ ($1 \leq n \leq 200000$) β the number of columns in the puzzle.
Following two lines describe the current arrangement on the puzzle. Each line contains $n$ integers, every one of which is either $0$ or $1$.
The last two lines describe Alice's desired arrangement in the same format.
-----Output-----
If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print $-1$.
-----Examples-----
Input
5
0 1 0 1 0
1 1 0 0 1
1 0 1 0 1
0 0 1 1 0
Output
5
Input
3
1 0 0
0 0 0
0 0 0
0 0 0
Output
-1
-----Note-----
In the first example the following sequence of swaps will suffice:
$(2, 1), (1, 1)$,
$(1, 2), (1, 3)$,
$(2, 2), (2, 3)$,
$(1, 4), (1, 5)$,
$(2, 5), (2, 4)$.
It can be shown that $5$ is the minimal possible answer in this case.
In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is $-1$. | n = int(input())
s = input()[::2], input()[::2]
t = input()[::2], input()[::2]
d = [0, 0]
total = 0
for y in range(n):
for x in (0, 1):
d[x] += (s[x][y] == "1") - (t[x][y] == "1")
if d[0] > 0 and d[1] < 0:
total += 1
d[0] -= 1
d[1] += 1
elif d[0] < 0 and d[1] > 0:
total += 1
d[0] += 1
d[1] -= 1
total += abs(d[0]) + abs(d[1])
print(total if d == [0, 0] else -1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR VAR STRING VAR VAR VAR STRING IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR LIST NUMBER NUMBER VAR NUMBER |
Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with $2$ rows and $n$ columns, every element of which is either $0$ or $1$. In one move you can swap two values in neighboring cells.
More formally, let's number rows $1$ to $2$ from top to bottom, and columns $1$ to $n$ from left to right. Also, let's denote a cell in row $x$ and column $y$ as $(x, y)$. We consider cells $(x_1, y_1)$ and $(x_2, y_2)$ neighboring if $|x_1 - x_2| + |y_1 - y_2| = 1$.
Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.
-----Input-----
The first line contains an integer $n$ ($1 \leq n \leq 200000$) β the number of columns in the puzzle.
Following two lines describe the current arrangement on the puzzle. Each line contains $n$ integers, every one of which is either $0$ or $1$.
The last two lines describe Alice's desired arrangement in the same format.
-----Output-----
If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print $-1$.
-----Examples-----
Input
5
0 1 0 1 0
1 1 0 0 1
1 0 1 0 1
0 0 1 1 0
Output
5
Input
3
1 0 0
0 0 0
0 0 0
0 0 0
Output
-1
-----Note-----
In the first example the following sequence of swaps will suffice:
$(2, 1), (1, 1)$,
$(1, 2), (1, 3)$,
$(2, 2), (2, 3)$,
$(1, 4), (1, 5)$,
$(2, 5), (2, 4)$.
It can be shown that $5$ is the minimal possible answer in this case.
In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is $-1$. | num = int(input())
m1 = [input().strip()[::2], input().strip()[::2]]
m2 = [input().strip()[::2], input().strip()[::2]]
top = 0
bot = 0
tot = 0
for j in range(num):
top += (m1[0][j] == "1") - (m2[0][j] == "1")
bot += (m1[1][j] == "1") - (m2[1][j] == "1")
if top > 0 and bot < 0:
tot += 1
top -= 1
bot += 1
elif top < 0 and bot > 0:
tot += 1
top += 1
bot -= 1
tot += abs(top) + abs(bot)
if top + bot != 0:
print(-1)
else:
print(tot) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL FUNC_CALL VAR NUMBER FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL FUNC_CALL VAR NUMBER FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with $2$ rows and $n$ columns, every element of which is either $0$ or $1$. In one move you can swap two values in neighboring cells.
More formally, let's number rows $1$ to $2$ from top to bottom, and columns $1$ to $n$ from left to right. Also, let's denote a cell in row $x$ and column $y$ as $(x, y)$. We consider cells $(x_1, y_1)$ and $(x_2, y_2)$ neighboring if $|x_1 - x_2| + |y_1 - y_2| = 1$.
Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.
-----Input-----
The first line contains an integer $n$ ($1 \leq n \leq 200000$) β the number of columns in the puzzle.
Following two lines describe the current arrangement on the puzzle. Each line contains $n$ integers, every one of which is either $0$ or $1$.
The last two lines describe Alice's desired arrangement in the same format.
-----Output-----
If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print $-1$.
-----Examples-----
Input
5
0 1 0 1 0
1 1 0 0 1
1 0 1 0 1
0 0 1 1 0
Output
5
Input
3
1 0 0
0 0 0
0 0 0
0 0 0
Output
-1
-----Note-----
In the first example the following sequence of swaps will suffice:
$(2, 1), (1, 1)$,
$(1, 2), (1, 3)$,
$(2, 2), (2, 3)$,
$(1, 4), (1, 5)$,
$(2, 5), (2, 4)$.
It can be shown that $5$ is the minimal possible answer in this case.
In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is $-1$. | import time
def main():
n = int(input())
curr = [[int(x) for x in input().split(" ")] for _ in range(2)]
want = [[int(x) for x in input().split(" ")] for _ in range(2)]
out = 0
s1 = 0
s2 = 0
for x in range(n):
out += abs(s1) + abs(s2)
s1 += curr[0][x]
s2 += curr[1][x]
s1 -= want[0][x]
s2 -= want[1][x]
if abs(s1 + s2) < abs(s1) + abs(s2):
if abs(s1) <= abs(s2):
out += abs(s1)
s2 += s1
s1 = 0
else:
out += abs(s2)
s1 += s2
s2 = 0
if s1 != 0 or s2 != 0:
print(-1)
else:
print(out)
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
There is a line consisting of N piles of coins on the table. For each i (1 β€ i β€ N), the i-th pile contains C_{i} coins, each with value V_{i}. There is also a bin under the table.
Alice and Bob play a game, moving alternately. A move consists of two steps:
Throw a prefix of the piles in the bin (the prefix can be empty). Then, if there are no coins left on the table, the game ends. Note that in this step, it is not allowed to throw away only some coins in a pile β the entire pile must be thrown into the bin.
Take exactly one coin from the first (remaining) pile. Then, if there are no coins left on the table, the game ends.
Let A and B be the sum of the values of the coins taken by Alice and Bob, respectively.
The score of the game is A - B. Alice wants to maximize the score; Bob wants to minimize it.
Alice moves first. What's the score of the game if Alice and Bob play optimally?
Please look at the sample cases below for an example of how the game is played.
------ Input Format ------
- The first line of input contains a single integer T, the number of test cases. The description of the test cases follows.
- Each test cases consists of three lines of input.
- The first line of each test case contains a single integer N, the number of piles of coins.
- The second line of each test case contains N space-separated integers C_{1}, C_{2}, \ldots, C_{N}: C_{i} is the number of coins in the i-th pile.
- The third line of each test case contains N space-separated integers V_{1}, V_{2}, \ldots, V_{N}: V_{i} is the value of each coin in the i-th pile.
------ Output Format ------
For each test case, print on a new line a single integer: the final value of A - B, if Alice and Bob play optimally.
------ Constraints ------
$1 β€T β€10^{4}$
$2 β€N β€2 \cdot 10^{5}$
$1 β€ C_{i} β€ 10^{9}$
$1 β€ V_{i} β€ 10^{9}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
6
1 2 5 1 1 3
38 46 66 64 59 69
6
2 5 4 1 1 2
49 24 17 54 23 50
2
4 2
32 78
----- Sample Output 1 ------
69
31
0
----- explanation 1 ------
Test case $1$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((1, 38), (2, 46), (5, 66), (1, 64), (1, 59), (3, 69))$.
- Alice throws the first $5$ piles in the bin. The piles are now $((3, 69))$. Then, she takes a coin from the first pile. The piles are now $((2, 69))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((1, 69))$.
- Alice doesn't throw any pile in the bin. Then, she takes a coin from the first pile. There are no piles left and the game ends.
- $A = 69 + 69 = 138$, $B = 69$, $A - B = 69$.
Test case $2$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((2, 49), (5, 24), (4, 17), (1, 54), (1, 23), (2, 50))$.
- Alice throws the first $3$ piles in the bin. The piles are now $((1, 54), (1, 23), (2, 50))$. Then, she takes a coin from the first pile. The piles are now $((1, 23), (2, 50))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((2, 50))$.
- Alice throws the first pile in the bin. There are no piles left and the game ends.
- $A = 54$, $B = 23$, $A - B = 31$. | import sys
sys.setrecursionlimit(10**6)
def f(arr, val, x, n, dp):
if x >= n:
return 0
if dp[x] != -1:
return dp[x]
a = 0
tm = 0
if dp[x + 1] == -1:
tm = f(arr, val, x + 1, n, dp)
dp[x + 1] = tm
else:
tm = dp[x + 1]
if arr[x] % 2 != 0:
a = val[x] - tm
b = tm
dp[x] = max(a, b)
return dp[x]
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
val = list(map(int, input().split()))
dp = [(-1) for i in range(n + 1)]
ans = f(arr, val, 0, n, dp)
print(ans) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
There is a line consisting of N piles of coins on the table. For each i (1 β€ i β€ N), the i-th pile contains C_{i} coins, each with value V_{i}. There is also a bin under the table.
Alice and Bob play a game, moving alternately. A move consists of two steps:
Throw a prefix of the piles in the bin (the prefix can be empty). Then, if there are no coins left on the table, the game ends. Note that in this step, it is not allowed to throw away only some coins in a pile β the entire pile must be thrown into the bin.
Take exactly one coin from the first (remaining) pile. Then, if there are no coins left on the table, the game ends.
Let A and B be the sum of the values of the coins taken by Alice and Bob, respectively.
The score of the game is A - B. Alice wants to maximize the score; Bob wants to minimize it.
Alice moves first. What's the score of the game if Alice and Bob play optimally?
Please look at the sample cases below for an example of how the game is played.
------ Input Format ------
- The first line of input contains a single integer T, the number of test cases. The description of the test cases follows.
- Each test cases consists of three lines of input.
- The first line of each test case contains a single integer N, the number of piles of coins.
- The second line of each test case contains N space-separated integers C_{1}, C_{2}, \ldots, C_{N}: C_{i} is the number of coins in the i-th pile.
- The third line of each test case contains N space-separated integers V_{1}, V_{2}, \ldots, V_{N}: V_{i} is the value of each coin in the i-th pile.
------ Output Format ------
For each test case, print on a new line a single integer: the final value of A - B, if Alice and Bob play optimally.
------ Constraints ------
$1 β€T β€10^{4}$
$2 β€N β€2 \cdot 10^{5}$
$1 β€ C_{i} β€ 10^{9}$
$1 β€ V_{i} β€ 10^{9}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
6
1 2 5 1 1 3
38 46 66 64 59 69
6
2 5 4 1 1 2
49 24 17 54 23 50
2
4 2
32 78
----- Sample Output 1 ------
69
31
0
----- explanation 1 ------
Test case $1$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((1, 38), (2, 46), (5, 66), (1, 64), (1, 59), (3, 69))$.
- Alice throws the first $5$ piles in the bin. The piles are now $((3, 69))$. Then, she takes a coin from the first pile. The piles are now $((2, 69))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((1, 69))$.
- Alice doesn't throw any pile in the bin. Then, she takes a coin from the first pile. There are no piles left and the game ends.
- $A = 69 + 69 = 138$, $B = 69$, $A - B = 69$.
Test case $2$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((2, 49), (5, 24), (4, 17), (1, 54), (1, 23), (2, 50))$.
- Alice throws the first $3$ piles in the bin. The piles are now $((1, 54), (1, 23), (2, 50))$. Then, she takes a coin from the first pile. The piles are now $((1, 23), (2, 50))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((2, 50))$.
- Alice throws the first pile in the bin. There are no piles left and the game ends.
- $A = 54$, $B = 23$, $A - B = 31$. | import sys
sys.setrecursionlimit(10**6)
def fun(i, n, l, x, c, dp):
if i == n:
return 0
if dp[i][x] != "a":
return dp[i][x]
if x == 1:
ans = min(
fun(i + 1, n, l, 1, c, dp), -l[i] * (c[i] % 2) + fun(i + 1, n, l, 0, c, dp)
)
else:
ans = max(
fun(i + 1, n, l, 0, c, dp), l[i] * (c[i] % 2) + fun(i + 1, n, l, 1, c, dp)
)
dp[i][x] = ans
return ans
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [["a", "a", "a"] for i in range(n + 1)]
print(fun(0, n, b, 0, a, dp)) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR STRING RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR |
There is a line consisting of N piles of coins on the table. For each i (1 β€ i β€ N), the i-th pile contains C_{i} coins, each with value V_{i}. There is also a bin under the table.
Alice and Bob play a game, moving alternately. A move consists of two steps:
Throw a prefix of the piles in the bin (the prefix can be empty). Then, if there are no coins left on the table, the game ends. Note that in this step, it is not allowed to throw away only some coins in a pile β the entire pile must be thrown into the bin.
Take exactly one coin from the first (remaining) pile. Then, if there are no coins left on the table, the game ends.
Let A and B be the sum of the values of the coins taken by Alice and Bob, respectively.
The score of the game is A - B. Alice wants to maximize the score; Bob wants to minimize it.
Alice moves first. What's the score of the game if Alice and Bob play optimally?
Please look at the sample cases below for an example of how the game is played.
------ Input Format ------
- The first line of input contains a single integer T, the number of test cases. The description of the test cases follows.
- Each test cases consists of three lines of input.
- The first line of each test case contains a single integer N, the number of piles of coins.
- The second line of each test case contains N space-separated integers C_{1}, C_{2}, \ldots, C_{N}: C_{i} is the number of coins in the i-th pile.
- The third line of each test case contains N space-separated integers V_{1}, V_{2}, \ldots, V_{N}: V_{i} is the value of each coin in the i-th pile.
------ Output Format ------
For each test case, print on a new line a single integer: the final value of A - B, if Alice and Bob play optimally.
------ Constraints ------
$1 β€T β€10^{4}$
$2 β€N β€2 \cdot 10^{5}$
$1 β€ C_{i} β€ 10^{9}$
$1 β€ V_{i} β€ 10^{9}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
6
1 2 5 1 1 3
38 46 66 64 59 69
6
2 5 4 1 1 2
49 24 17 54 23 50
2
4 2
32 78
----- Sample Output 1 ------
69
31
0
----- explanation 1 ------
Test case $1$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((1, 38), (2, 46), (5, 66), (1, 64), (1, 59), (3, 69))$.
- Alice throws the first $5$ piles in the bin. The piles are now $((3, 69))$. Then, she takes a coin from the first pile. The piles are now $((2, 69))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((1, 69))$.
- Alice doesn't throw any pile in the bin. Then, she takes a coin from the first pile. There are no piles left and the game ends.
- $A = 69 + 69 = 138$, $B = 69$, $A - B = 69$.
Test case $2$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((2, 49), (5, 24), (4, 17), (1, 54), (1, 23), (2, 50))$.
- Alice throws the first $3$ piles in the bin. The piles are now $((1, 54), (1, 23), (2, 50))$. Then, she takes a coin from the first pile. The piles are now $((1, 23), (2, 50))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((2, 50))$.
- Alice throws the first pile in the bin. There are no piles left and the game ends.
- $A = 54$, $B = 23$, $A - B = 31$. | T = int(input())
for i in range(T):
N = int(input())
C = [int(x) for x in input().split()]
V = [int(x) for x in input().split()]
F = []
for i in range(N):
if C[i] % 2 == 1:
F += [V[i]]
M = len(F)
f = 0
g = 0
for i in range(M - 1, -1, -1):
h = max(g + F[i], f)
g = min(f - F[i], g)
f = h
print(f) | 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
There is a line consisting of N piles of coins on the table. For each i (1 β€ i β€ N), the i-th pile contains C_{i} coins, each with value V_{i}. There is also a bin under the table.
Alice and Bob play a game, moving alternately. A move consists of two steps:
Throw a prefix of the piles in the bin (the prefix can be empty). Then, if there are no coins left on the table, the game ends. Note that in this step, it is not allowed to throw away only some coins in a pile β the entire pile must be thrown into the bin.
Take exactly one coin from the first (remaining) pile. Then, if there are no coins left on the table, the game ends.
Let A and B be the sum of the values of the coins taken by Alice and Bob, respectively.
The score of the game is A - B. Alice wants to maximize the score; Bob wants to minimize it.
Alice moves first. What's the score of the game if Alice and Bob play optimally?
Please look at the sample cases below for an example of how the game is played.
------ Input Format ------
- The first line of input contains a single integer T, the number of test cases. The description of the test cases follows.
- Each test cases consists of three lines of input.
- The first line of each test case contains a single integer N, the number of piles of coins.
- The second line of each test case contains N space-separated integers C_{1}, C_{2}, \ldots, C_{N}: C_{i} is the number of coins in the i-th pile.
- The third line of each test case contains N space-separated integers V_{1}, V_{2}, \ldots, V_{N}: V_{i} is the value of each coin in the i-th pile.
------ Output Format ------
For each test case, print on a new line a single integer: the final value of A - B, if Alice and Bob play optimally.
------ Constraints ------
$1 β€T β€10^{4}$
$2 β€N β€2 \cdot 10^{5}$
$1 β€ C_{i} β€ 10^{9}$
$1 β€ V_{i} β€ 10^{9}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
6
1 2 5 1 1 3
38 46 66 64 59 69
6
2 5 4 1 1 2
49 24 17 54 23 50
2
4 2
32 78
----- Sample Output 1 ------
69
31
0
----- explanation 1 ------
Test case $1$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((1, 38), (2, 46), (5, 66), (1, 64), (1, 59), (3, 69))$.
- Alice throws the first $5$ piles in the bin. The piles are now $((3, 69))$. Then, she takes a coin from the first pile. The piles are now $((2, 69))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((1, 69))$.
- Alice doesn't throw any pile in the bin. Then, she takes a coin from the first pile. There are no piles left and the game ends.
- $A = 69 + 69 = 138$, $B = 69$, $A - B = 69$.
Test case $2$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((2, 49), (5, 24), (4, 17), (1, 54), (1, 23), (2, 50))$.
- Alice throws the first $3$ piles in the bin. The piles are now $((1, 54), (1, 23), (2, 50))$. Then, she takes a coin from the first pile. The piles are now $((1, 23), (2, 50))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((2, 50))$.
- Alice throws the first pile in the bin. There are no piles left and the game ends.
- $A = 54$, $B = 23$, $A - B = 31$. | t = int(input())
for _ in range(t):
n = int(input())
c = list(map(int, input().split()))
v = list(map(int, input().split()))
maxt = 0
for i in range(n - 1, -1, -1):
if c[i] % 2 == 0:
continue
maxt = max(maxt, v[i] - maxt)
print(maxt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There is a line consisting of N piles of coins on the table. For each i (1 β€ i β€ N), the i-th pile contains C_{i} coins, each with value V_{i}. There is also a bin under the table.
Alice and Bob play a game, moving alternately. A move consists of two steps:
Throw a prefix of the piles in the bin (the prefix can be empty). Then, if there are no coins left on the table, the game ends. Note that in this step, it is not allowed to throw away only some coins in a pile β the entire pile must be thrown into the bin.
Take exactly one coin from the first (remaining) pile. Then, if there are no coins left on the table, the game ends.
Let A and B be the sum of the values of the coins taken by Alice and Bob, respectively.
The score of the game is A - B. Alice wants to maximize the score; Bob wants to minimize it.
Alice moves first. What's the score of the game if Alice and Bob play optimally?
Please look at the sample cases below for an example of how the game is played.
------ Input Format ------
- The first line of input contains a single integer T, the number of test cases. The description of the test cases follows.
- Each test cases consists of three lines of input.
- The first line of each test case contains a single integer N, the number of piles of coins.
- The second line of each test case contains N space-separated integers C_{1}, C_{2}, \ldots, C_{N}: C_{i} is the number of coins in the i-th pile.
- The third line of each test case contains N space-separated integers V_{1}, V_{2}, \ldots, V_{N}: V_{i} is the value of each coin in the i-th pile.
------ Output Format ------
For each test case, print on a new line a single integer: the final value of A - B, if Alice and Bob play optimally.
------ Constraints ------
$1 β€T β€10^{4}$
$2 β€N β€2 \cdot 10^{5}$
$1 β€ C_{i} β€ 10^{9}$
$1 β€ V_{i} β€ 10^{9}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
6
1 2 5 1 1 3
38 46 66 64 59 69
6
2 5 4 1 1 2
49 24 17 54 23 50
2
4 2
32 78
----- Sample Output 1 ------
69
31
0
----- explanation 1 ------
Test case $1$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((1, 38), (2, 46), (5, 66), (1, 64), (1, 59), (3, 69))$.
- Alice throws the first $5$ piles in the bin. The piles are now $((3, 69))$. Then, she takes a coin from the first pile. The piles are now $((2, 69))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((1, 69))$.
- Alice doesn't throw any pile in the bin. Then, she takes a coin from the first pile. There are no piles left and the game ends.
- $A = 69 + 69 = 138$, $B = 69$, $A - B = 69$.
Test case $2$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((2, 49), (5, 24), (4, 17), (1, 54), (1, 23), (2, 50))$.
- Alice throws the first $3$ piles in the bin. The piles are now $((1, 54), (1, 23), (2, 50))$. Then, she takes a coin from the first pile. The piles are now $((1, 23), (2, 50))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((2, 50))$.
- Alice throws the first pile in the bin. There are no piles left and the game ends.
- $A = 54$, $B = 23$, $A - B = 31$. | from sys import setrecursionlimit, stdin
input = stdin.readline
setrecursionlimit(5 * 10**5)
inp = lambda: list(map(int, input().split()))
def solve(i, turn):
if i == n:
return 0
if dp[i][turn] != None:
return dp[i][turn]
if turn == 0:
ans = solve(nind[i], 1) + b[i]
ans = max(ans, solve(nind[i], 0))
else:
ans = solve(nind[i], 0) - b[i]
ans = min(ans, solve(nind[i], 1))
dp[i][turn] = ans
return ans
def answer():
global dp, nind
nind = [n for i in range(n)]
s = []
for i in range(n):
while len(s) and a[i] & 1:
nind[s.pop()] = i
s.append(i)
dp = [[None, None] for i in range(n)]
start = n
for i in range(n):
if a[i] & 1:
start = i
break
ans = solve(start, 0)
return ans
for T in range(int(input())):
n = int(input())
a = inp()
b = inp()
print(answer()) | ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NONE NONE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
There is a line consisting of N piles of coins on the table. For each i (1 β€ i β€ N), the i-th pile contains C_{i} coins, each with value V_{i}. There is also a bin under the table.
Alice and Bob play a game, moving alternately. A move consists of two steps:
Throw a prefix of the piles in the bin (the prefix can be empty). Then, if there are no coins left on the table, the game ends. Note that in this step, it is not allowed to throw away only some coins in a pile β the entire pile must be thrown into the bin.
Take exactly one coin from the first (remaining) pile. Then, if there are no coins left on the table, the game ends.
Let A and B be the sum of the values of the coins taken by Alice and Bob, respectively.
The score of the game is A - B. Alice wants to maximize the score; Bob wants to minimize it.
Alice moves first. What's the score of the game if Alice and Bob play optimally?
Please look at the sample cases below for an example of how the game is played.
------ Input Format ------
- The first line of input contains a single integer T, the number of test cases. The description of the test cases follows.
- Each test cases consists of three lines of input.
- The first line of each test case contains a single integer N, the number of piles of coins.
- The second line of each test case contains N space-separated integers C_{1}, C_{2}, \ldots, C_{N}: C_{i} is the number of coins in the i-th pile.
- The third line of each test case contains N space-separated integers V_{1}, V_{2}, \ldots, V_{N}: V_{i} is the value of each coin in the i-th pile.
------ Output Format ------
For each test case, print on a new line a single integer: the final value of A - B, if Alice and Bob play optimally.
------ Constraints ------
$1 β€T β€10^{4}$
$2 β€N β€2 \cdot 10^{5}$
$1 β€ C_{i} β€ 10^{9}$
$1 β€ V_{i} β€ 10^{9}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
6
1 2 5 1 1 3
38 46 66 64 59 69
6
2 5 4 1 1 2
49 24 17 54 23 50
2
4 2
32 78
----- Sample Output 1 ------
69
31
0
----- explanation 1 ------
Test case $1$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((1, 38), (2, 46), (5, 66), (1, 64), (1, 59), (3, 69))$.
- Alice throws the first $5$ piles in the bin. The piles are now $((3, 69))$. Then, she takes a coin from the first pile. The piles are now $((2, 69))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((1, 69))$.
- Alice doesn't throw any pile in the bin. Then, she takes a coin from the first pile. There are no piles left and the game ends.
- $A = 69 + 69 = 138$, $B = 69$, $A - B = 69$.
Test case $2$: An optimal strategy for Alice and Bob is the following:
- Initially, the piles, denoted by $(C_{i}, V_{i})$, are $((2, 49), (5, 24), (4, 17), (1, 54), (1, 23), (2, 50))$.
- Alice throws the first $3$ piles in the bin. The piles are now $((1, 54), (1, 23), (2, 50))$. Then, she takes a coin from the first pile. The piles are now $((1, 23), (2, 50))$.
- Bob doesn't throw any pile in the bin. Then, he takes a coin from the first pile. The piles are now $((2, 50))$.
- Alice throws the first pile in the bin. There are no piles left and the game ends.
- $A = 54$, $B = 23$, $A - B = 31$. | import sys
sys.setrecursionlimit(1000000)
def mi():
return map(int, input().split())
def li():
return list(mi())
def si():
return str(input())
def ni():
return int(input())
def printyes():
print("YES")
def printno():
print("NO")
for t in range(int(input())):
n = ni()
c = li()
v = li()
c.reverse()
v.reverse()
ans = [0]
maxx = [0]
for i in range(n):
if c[i] % 2 == 1:
curr = v[i] - maxx[-1]
ans.append(max(curr, maxx[-1]))
maxx.append(max(maxx[-1], ans[-1]))
print(ans[-1]) | IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | 3
n = int(input())
books = [[], []]
for _ in range(n):
t, w = tuple(map(int, input().split()))
books[t - 1].append(w)
for _ in range(2):
books[_].sort()
ans = 10**9
for i in range(len(books[0]) + 1):
for j in range(len(books[1]) + 1):
hor = sum(books[0][:i]) + sum(books[1][:j])
ver = len(books[0]) - i + 2 * (len(books[1]) - j)
if hor <= ver and ver < ans:
ans = ver
print(ans) | EXPR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
t = []
for i in range(n):
e = list(map(int, input().split()))
t.append(e)
size = 0
size2 = 0
for i in t:
size += i[0]
size2 += i[1]
matrix = [([-1000] * (size + 1)) for i in range(n)]
for i in range(n):
matrix[i][0] = 0
for i in range(n):
for j in range(1, size + 1):
if t[i][0] <= j:
if t[i][0] == j and matrix[i - 1][j] == 1000:
matrix[i][j] = t[i][1]
else:
matrix[i][j] = max(
matrix[i - 1][j], matrix[i - 1][j - t[i][0]] + t[i][1]
)
else:
matrix[i][j] = matrix[i - 1][j]
for i in range(1, size + 1):
if i >= size2 - matrix[-1][i]:
print(i)
break | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
t = [0] * n
w = [0] * n
for i in range(n):
t[i], w[i] = map(int, input().split())
dp = [([float("inf")] * (2 * n + 1)) for i in range(n)]
dp[0][t[0]] = 0
dp[0][0] = w[0]
for i in range(n - 1):
for j in range(2 * n + 1):
a = dp[i][j]
if a != float("inf"):
dp[i + 1][j + t[i + 1]] = min(dp[i + 1][j + t[i + 1]], dp[i][j])
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + w[i + 1])
ans = float("inf")
for j in range(2 * n + 1):
if dp[n - 1][j] <= j:
ans = min(ans, j)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
t, w = [], []
for i in range(n):
a, b = map(int, input().split())
t.append(a)
w.append(b)
d = [([float("-inf")] * (2 * n + 1)) for i in range(n + 1)]
d[0][0] = 0
for i in range(n):
for j in range(2 * n + 1):
if j + t[i] <= 2 * n:
d[i + 1][j + t[i]] = max(d[i + 1][j + t[i]], d[i][j] + w[i])
d[i + 1][j] = max(d[i + 1][j], d[i][j])
for j in range(2 * n + 1):
if sum(w) - d[n][j] <= j:
print(j)
return | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
lis = []
for _ in range(n):
tt, ww = [int(x) for x in input().split()]
lis.append((tt, -ww))
lis.sort()
sum1, sum2 = [0], [0]
for i in range(n):
if lis[i][0] == 1:
sum1.append(sum1[-1] - lis[i][1])
else:
sum2.append(sum2[-1] - lis[i][1])
ans = 1000000000
k1, k2 = len(sum1) - 1, len(sum2) - 1
v = k2 + 1
for u in range(0, k1 + 1):
while v - 1 >= 0 and 2 * (v - 1) + sum2[v - 1] >= sum1[k1] + sum2[k2] - sum1[u] - u:
v -= 1
if v <= k2:
ans = min(ans, u + 2 * v)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
a = list(tuple(map(int, input().split())) for i in range(n))
s = sum(t for t, w in a)
a = [(t / w, t, t + w) for t, w in a]
a.sort(reverse=True)
d, i = s, 0
while d >= 0:
s -= a[i][1]
d -= a[i][2]
i += 1
i -= 1
s += a[i][1]
d += a[i][2]
if a[i][1] == 2:
j = i + 1
while j < n and a[j][1] == 2:
j += 1
if j < n and d >= a[j][2]:
i = 0
s -= 1
if i > 0:
i -= 1
if a[i][1] == 1:
d += a[i][2]
j = i + 1
while j < n and a[j][1] == 1:
j += 1
if j < n and d >= a[j][2]:
s -= 1
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
m = 2 * n + 1
res = set([(0, 0)])
for i in range(n):
t, w = map(int, input().split())
nxt = set()
for a, b in res:
if a + t <= m:
nxt.add((a + t, b))
if b + w <= m:
nxt.add((a, b + w))
res = nxt
print(min(t for t, w in res if t >= w)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | 3
n = int(input())
t = []
w = []
n1, n2 = 0, 0
for i in range(n):
x, y = list(map(int, input().split()))
t.append(x)
w.append(y)
n1 += x == 1
n2 += x == 2
w1 = []
w2 = []
for i in range(n):
if t[i] == 1:
w1.append(w[i])
else:
w2.append(w[i])
w1.sort(reverse=True)
w2.sort(reverse=True)
ans = 201
for i in range(201):
for j in range(min(i // 2, n2) + 1):
k = min(n1, i - 2 * j)
thickness = 2 * j + k
if thickness >= sum(w1[k:]) + sum(w2[j:]):
ans = min(ans, thickness)
print(ans) | EXPR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | R = lambda: map(int, input().split())
n = int(input())
unset = -1000000
bs = [[0, 0]] + [list(R()) for i in range(n)]
dp = [([unset] * 201) for i in range(n + 1)]
dp[0][0] = 0
for i in range(n):
for j in range(201):
t, w = bs[i + 1][0], bs[i + 1][1]
if dp[i][j] != unset:
dp[i + 1][j + t] = max(dp[i + 1][j + t], dp[i][j] + t)
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - w)
for i in range(201):
if dp[n][i] >= 0:
print(i)
break | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
dif = []
li1 = []
li2 = []
a1 = b1 = 0
for i in range(n):
a, b = map(int, input().split())
if a == 1:
li1.append(b)
else:
li2.append(b)
li1.sort(reverse=True)
li2.sort(reverse=True)
l1 = len(li1)
l2 = len(li2)
ans = 10000000000
for i in range(l1 + 1):
for j in range(l2 + 1):
s = 0
for k in range(i, l1):
s += li1[k]
for k in range(j, l2):
s += li2[k]
if i + 2 * j >= s:
ans = min(ans, i + 2 * j)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
s1 = sum([x[0] for x in a])
a.sort(key=lambda x: x[1] / x[0])
ans = 10**9
s2 = 0
l = -1
for i in range(n):
if s2 + a[i][1] <= s1 - a[i][0]:
s2 += a[i][1]
s1 -= a[i][0]
l = i
if l != -1 and a[l][0] == 1:
for i in range(l + 1, n):
if a[i][0] == 2 and s2 - a[l][1] + a[i][1] <= s1 - 1:
print(s1 - 1)
exit(0)
print(s1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
t1 = []
t2 = []
for _ in range(n):
t, w = map(int, input().split())
if t == 1:
t1.append(w)
else:
t2.append(w)
t1.sort()
t2.sort()
n1 = len(t1)
n2 = len(t2)
p1 = [0]
p2 = [0]
for i in range(n1):
p1.append(p1[-1] + t1[i])
for i in range(n2):
p2.append(t2[i] + p2[-1])
ans = 1
while True:
am = False
b = 0
while 2 * b <= ans:
a = ans - 2 * b
if a <= n1 and b <= n2:
if p1[n1 - a] + p2[n2 - b] <= ans:
am = True
break
b += 1
if am:
print(ans)
break
ans += 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
tOne = [0]
tTwo = [0]
sizeOne = 1
sizetwo = 1
slon = 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
for i in range(n):
t = input().split()
if t[0] == "1":
tOne.append(int(t[1]))
sizeOne += 1
else:
tTwo.append(int(t[1]))
sizetwo += 1
tOne.sort(reverse=False)
tTwo.sort(reverse=False)
for i in range(1, sizeOne):
tOne[i] += tOne[i - 1]
for i in range(1, sizetwo):
tTwo[i] += tTwo[i - 1]
tOne.reverse()
tTwo.reverse()
for i in range(sizeOne):
for j in range(sizetwo):
if i + 2 * j >= tOne[i] + tTwo[j]:
slon = min(slon, i + 2 * j)
print(slon) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | one = []
two = []
x = int(input())
for i in range(x):
a, b = list(map(int, input().split(" ")))
if a == 1:
one.append(b)
else:
two.append(b)
one.sort()
two.sort()
minx = 9000000
for ones in range(0, len(one) + 1):
for twos in range(0, len(two) + 1):
w = ones + 2 * twos
a = sum(one[: len(one) - ones]) + sum(two[: len(two) - twos])
if a <= w:
minx = min(minx, w)
print(minx) | ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image]
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
-----Input-----
The first line of the input contains an integer n, (1 β€ n β€ 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 β€ t_{i} β€ 2, 1 β€ w_{i} β€ 100).
-----Output-----
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
-----Examples-----
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | n = int(input())
width, thickness = [], []
one, two = [], []
for _ in range(n):
ti, wi = map(int, input().split())
thickness.append(ti), width.append(wi)
if ti == 1:
one.append(wi)
if ti == 2:
two.append(wi)
one.sort(), two.sort()
one = [0] + one
two = [0] + two
for i in range(1, len(one)):
one[i] = one[i - 1] + one[i]
for i in range(1, len(two)):
two[i] = two[i - 1] + two[i]
ans = 1e19
for i in range(len(one)):
for ii in range(len(two)):
thick = i + 2 * ii
width = one[len(one) - i - 1]
width += two[len(two) - ii - 1]
if width <= thick:
ans = min(ans, thick)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR 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 BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | from sys import *
input = lambda: stdin.readline()
int_arr = lambda: list(map(int, stdin.readline().strip().split()))
str_arr = lambda: list(map(str, stdin.readline().split()))
get_str = lambda: map(str, stdin.readline().strip().split())
get_int = lambda: map(int, stdin.readline().strip().split())
get_float = lambda: map(float, stdin.readline().strip().split())
mod = 1000000007
setrecursionlimit(1000)
n = int(input())
res = []
while n:
temp = n
tot = 0
mul = 1
while temp:
rem = temp % 10
if rem:
tot += mul
mul *= 10
temp //= 10
res += [tot]
n -= tot
print(len(res))
print(*res) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | def quasiBinary(n):
ans = list()
while n:
tmp = str()
for i in str(n):
if int(i) >= 1:
tmp += "1"
else:
tmp += "0"
ans.append(tmp)
n -= int(tmp)
print(len(ans))
for i in ans:
print(i, end=" ")
n = int(input())
quasiBinary(n) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | num = input()
res = 0
arr = []
for i in num:
res = max(res, int(i))
arr.append(int(i))
s = ""
print(res)
for i in range(res):
numCur = 0
cur = ""
for j in range(len(arr)):
if arr[j] != 0:
cur += "1"
arr[j] -= 1
elif cur != "":
cur += "0"
s += cur + " "
print(s) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR STRING VAR VAR NUMBER IF VAR STRING VAR STRING VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = int(input())
m = 0
aa = [0] * 10005
t = 1
while n:
x = n % 10
m = max(m, x)
for i in range(x):
aa[i] += t
t *= 10
n //= 10
print(m)
for i in range(m):
print(aa[i], end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = input()
maxx = 0
for i in n:
maxx = max(maxx, int(i))
d = [[(0) for i in range(len(n))] for j in range(maxx)]
for i in range(len(n)):
for j in range(int(n[i])):
d[j][i] = 1
print(maxx)
for i in d:
s = ""
for j in i:
if j == 0 and len(s) == 0:
continue
s += str(j)
print(s, end=" ") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = int(input())
nums = str(n)
m = -1
for i in nums:
m = max(m, int(i))
ans = [[(0) for i in range(len(nums))] for j in range(m)]
c = 0
for i in nums:
cur = int(i)
for i in range(cur):
ans[i][c] = 1
c += 1
print(len(ans))
for i in ans:
c = ""
for j in i:
c += str(j)
print(int(c)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | def main(k):
num = list(map(int, k))
res = []
while True:
st = []
flag = False
for i in range(len(num)):
if num[i] > 0:
st.append("1")
num[i] -= 1
flag = True
else:
st.append("0")
if not flag:
break
res.append(str(int("".join(st))))
print(str(len(res)))
print(" ".join(res))
def __starting_point():
s = input()
main(s)
__starting_point() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = int(input())
L = []
while n > 0:
cb = "".join([["0", "1"][int(k) > 0] for k in str(n)])
L.append(cb)
n -= int(cb)
print(len(L))
print(" ".join(L)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL STRING LIST STRING STRING FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = input()
def ans(n):
li = []
maxm = int(n[0])
for i in n:
li.append(int(i))
maxm = max(maxm, int(i))
print(maxm)
l = len(li)
deepshri = []
for i in range(maxm):
s = ""
for j in range(l):
if li[j] != 0:
s += "1"
li[j] -= 1
else:
s += "0"
deepshri.append(s)
for i in deepshri:
print(int(i), end=" ")
ans(n) | ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR STRING VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = int(input())
l = []
while n != 0:
b = ""
for i in str(n):
b += str(min(int(i), 1))
n -= int(b)
l.append(b)
print(len(l))
print(*l) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | intab = "0123456789"
outtab = "0111111111"
trantab = str.maketrans(intab, outtab)
def quasi(x):
return int(x.translate(trantab))
ans = []
n = int(input())
while n > 0:
val = quasi(str(n))
n -= val
ans.append(val)
print(len(ans))
print(" ".join(map(str, ans))) | ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = input()
m = max(int(x) for x in n)
ls = [""] * m
for x in n:
y = int(x)
for i in range(y):
ls[i] += "1"
for i in range(y, m):
ls[i] += "0"
print(m)
for x in ls:
print(int(x), end=" ") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = input()
l = len(n)
ans = []
for i in range(len(n)):
ans.append(int(n[i:]) // 10 ** (len(n[i:]) - 1))
out = []
while any(ans):
temp = []
for i in range(l):
if ans[i] > 0:
temp.append(10 ** (l - i - 1))
ans[i] -= 1
out.append(sum(temp))
print(len(out))
print(*out) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | p = [
1,
10,
11,
100,
101,
110,
111,
1000,
1001,
1010,
1011,
1100,
1101,
1110,
1111,
10000,
10001,
10010,
10011,
10100,
10101,
10110,
10111,
11000,
11001,
11010,
11011,
11100,
11101,
11110,
11111,
100000,
100001,
100010,
100011,
100100,
100101,
100110,
100111,
101000,
101001,
101010,
101011,
101100,
101101,
101110,
101111,
110000,
110001,
110010,
110011,
110100,
110101,
110110,
110111,
111000,
111001,
111010,
111011,
111100,
111101,
111110,
111111,
1000000,
]
n = input()
f = 0
for j in range(len(p)):
if p[j] <= int(n):
f = j
u = []
x = 0
for j in range(len(n) - 1, -1, -1):
a = int(n[j])
if x == 0:
if a == 0:
u = ["0"]
else:
u = ["1"] * a
x += 1
else:
g = len(u) - 1
for i in range(a):
if g >= 0:
u[g] = "1" + u[g]
g -= 1
else:
u.insert(0, "1" + "0" * (len(u[0]) - 1))
if g >= 0:
for r in range(g, -1, -1):
u[r] = "0" + u[r]
print(len(u))
for j in range(len(u)):
u[j] = int(u[j])
print(*u) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST STRING ASSIGN VAR BIN_OP LIST STRING VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP STRING BIN_OP STRING BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = int(input())
c = 0
l = []
while n > 0:
c = c + 1
temp = n
m = 0
p = 1
while temp:
rem = temp % 10
temp = int(temp / 10)
if rem != 0:
m += p
p *= 10
l.append(m)
n = n - m
print(c)
print(*l) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = list(map(int, list(input())))
_max = max(n)
print(_max)
for i in range(_max):
l = [0] * len(n)
for j in range(len(n)):
if n[j] > 0:
l[j] = 1
n[j] -= 1
else:
l[j] = 0
for j in range(len(l)):
if l[j] == 1:
l = l[j:]
break
print("".join(list(map(str, l))), end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = int(input())
s = list(map(int, str(n)))
print(max(s))
l = len(s)
for i in range(max(s)):
f = 0
for i in range(l):
if s[i] > 0:
s[i] -= 1
print(1, end="")
f = 1
elif f:
print(0, end="")
print(" ", end="") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR STRING STRING |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | __author__ = "ploskov"
def termination_condition(digit_list: "list"):
for digit in digit_list:
if digit > 0:
return False
return True
def main():
number = input()
digit_list = [int(digit) for digit in number]
answer = []
while not termination_condition(digit_list):
sub_number = [(0) for digit in number]
for index in range(len(digit_list)):
if digit_list[index] > 0:
sub_number[index] = 1
digit_list[index] -= 1
answer.append(str(int("".join(map(str, sub_number)))))
print(len(answer))
print(" ".join(answer))
main() | ASSIGN VAR STRING FUNC_DEF STRING FOR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = input().strip()
a = list(n)
c = [int(i) for i in a]
results = []
while sum(c) > 0:
qb = [(0) for i in range(len(c))]
for i in range(len(c)):
if c[i] > 0:
qb[i] = 1
c[i] -= 1
qbs = int("".join([str(c) for c in qb]))
results.append(qbs)
print(len(results))
print(" ".join([str(num) for num in results])) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = int(input())
inf = 10**9 + 9
ans = []
while n > 0:
j = n
b = 0
t = 1
while j > 0:
b += t * int(j % 10 != 0)
t *= 10
j //= 10
ans.append(b)
n -= b
print(len(ans))
for i in ans:
print(i, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = input()
k = []
s = ""
a = [int(i) for i in n]
for i in range(max(a)):
for j in range(len(a)):
if a[j] > 0:
s += "1"
else:
s += "0"
a[j] -= 1
k.append(int(s))
s = ""
print(len(k))
for i in range(len(k)):
print(k[i], end=" ") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR STRING VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | N = int(input())
l = []
while N:
n = N
m = 0
p = 1
while n:
if n % 10:
m += p
n //= 10
p *= 10
l.append(m)
N -= m
l.sort()
print(len(l))
print(*l) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = input()
l = []
for i in range(9):
s = ""
for j in n:
if ord(j) - i - 48 > 0:
s += "1"
else:
s += "0"
l.append(s)
for i in range(9):
l[i] = int(l[i])
while True:
try:
l.remove(0)
except:
break
print(len(l))
print(*l) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR WHILE NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | def main():
num = [int(x) for x in list(input())]
quasi_binary = []
s = ""
for i in range(max(num)):
for j in range(len(num)):
if num[j] > 0:
s += "1"
num[j] -= 1
else:
s += "0"
quasi_binary.append(str(int(s)))
s = ""
print(len(quasi_binary))
print(" ".join(quasi_binary))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR STRING VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = int(input())
l = len(str(n)) - 1
k = int(str(n)[0:1])
a = [10**l] * k
n = n - k * a[0]
while n > 0:
l = len(str(n)) - 1
k = int(str(n)[0:1])
b = 10**l
if k > len(a):
z = k - len(a)
for i in range(z):
a.append(b)
n = n - b
for i in range(len(a)):
if n - b >= 0:
a[i] = a[i] + b
n = n - b
else:
for i in range(k):
a[i] = a[i] + b
n = n - k * b
print(len(a))
print(*a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | a = input()
a = list(a)
for i in range(len(a)):
a[i] = int(a[i])
ans = max(a)
print(ans)
for j in range(ans):
aa = [0] * len(a)
for i in range(len(a)):
if a[i] != 0:
aa[i] = 1
a[i] -= 1
print(int("".join(str(x) for x in aa)), end=" ") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING |
A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011Β β are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^6).
-----Output-----
In the first line print a single integer kΒ β the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers β the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.
-----Examples-----
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11 | n = int(input())
a = [0] * 10
t = 1
m = 1
while n > 0:
d = n % 10
for i in range(d):
a[i] += t
n //= 10
t *= 10
m = max(m, d)
print(m)
for i in a:
if i != 0:
print(i, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.