description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a trie of all correct bracket sequences of length $2n$.
The definition of correct bracket sequence is as follows: The empty sequence is a correct bracket sequence, If $s$ is a correct bracket sequence, then $(\,s\,)$ is a correct bracket sequence, If $s$ and $t$ are a correct bracket sequence, then $st$ is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo $10^9 + 7$.
-----Input-----
The only line contains a single integer $n$ ($1 \le n \le 1000$).
-----Output-----
Print exactly one integerΒ β the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo $10^9 + 7$.
-----Examples-----
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
-----Note-----
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue. [Image]Β [Image] | N = int(input())
mod = 10**9 + 7
dp = [([0] * (N + 1)) for _ in range(N + 1)]
dp[1][0] = 1
dp[1][1] = 1
for i in range(2, N + 1):
if i % 2:
dp[i][i] = 1 + i // 2
else:
dp[i][i] = i // 2
for j in range(i - 1, -1, -1):
dp[i][j] = (dp[i][j + 1] + dp[i - 1][j - 1] + j % 2) % mod
print(dp[-1][0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER |
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a trie of all correct bracket sequences of length $2n$.
The definition of correct bracket sequence is as follows: The empty sequence is a correct bracket sequence, If $s$ is a correct bracket sequence, then $(\,s\,)$ is a correct bracket sequence, If $s$ and $t$ are a correct bracket sequence, then $st$ is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo $10^9 + 7$.
-----Input-----
The only line contains a single integer $n$ ($1 \le n \le 1000$).
-----Output-----
Print exactly one integerΒ β the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo $10^9 + 7$.
-----Examples-----
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
-----Note-----
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue. [Image]Β [Image] | N = int(input())
MOD = 10**9 + 7
dp = [([0] * (2020 + 1)) for _ in range(2020 + 1)]
dp[1][1] = 1
ans = 0
for i in range(2, N + 2):
for j in range(1, i + 1):
dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD
if (i + j) % 2 == 1:
ans = (ans + dp[i][j]) % MOD
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a trie of all correct bracket sequences of length $2n$.
The definition of correct bracket sequence is as follows: The empty sequence is a correct bracket sequence, If $s$ is a correct bracket sequence, then $(\,s\,)$ is a correct bracket sequence, If $s$ and $t$ are a correct bracket sequence, then $st$ is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo $10^9 + 7$.
-----Input-----
The only line contains a single integer $n$ ($1 \le n \le 1000$).
-----Output-----
Print exactly one integerΒ β the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo $10^9 + 7$.
-----Examples-----
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
-----Note-----
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue. [Image]Β [Image] | n = int(input())
mod = 10**9 + 7
dp = [([0] * (n + 1)) for i in range(2 * n + 1)]
dp[0][0] = 1
for d in range(1, 2 * n + 1):
for v in range(n + 1):
if v + 1 <= n:
dp[d][v + 1] += dp[d - 1][v]
dp[d][v + 1] %= mod
if v - 1 >= 0:
dp[d][v - 1] += dp[d - 1][v]
dp[d][v - 1] %= mod
ans = 0
for d in range(2 * n + 1):
if d % 2 == 1:
for v in range(n + 1):
if v <= 2 * n - d:
ans += dp[d][v]
ans %= mod
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Bengali], [Russian], [Mandarin] and [Vietnamese] as well.
The *functional array* of an array A = [A_{1}, A_{2}, \dots, A_{N}] is the array fA of size N-1, where fA_{i} = A_{i+1} - A_{i} for 1β€ i < N. For example, if A = [2, 3, 9, 11] then fA = [1, 6, 2].
You are given two arrays B = [B_{1}, B_{2}, \dots, B_{N}] and Z = [Z_{1}, Z_{2}, \dots, Z_{M}] of length N and M respectively. Find out whether there exists an array A such that:
B is a [subsequence] of A, and
fA is a subsequence of Z
Print "YES" (without quotes) if such an array A exists, and "NO" otherwise.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- Each test case contains three lines of input.
- The first line contains two integers N and M - the lengths of B and Z respectively.
- The second line contains N space-separated integers B_{1}, B_{2}, \dots, B_{N}; the elements of B.
- The third line contains M space-separated integers Z_{1}, Z_{2}, \dots, Z_{M}; the elements of Z.
------ Output Format ------
For each test case, output a single line containing one word - either "YES" or "NO". You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ M β€ 10^{5}$
$0 β€ B_{i} β€ 10^{5}$
$0 β€ Z_{i} β€ 10^{5}$
- The sum of $N$ over all test cases does not exceed $10^{5}$
- The sum of $M$ over all test cases does not exceed $10^{5}$
------ subtasks ------
Subtask #1 (5 points):
$1β€ M β€ 10$
$1β€ N β€ 10^{3}$
$0β€ B_{i} β€ 10^{3}$
$0β€ Z_{i} β€ 10^{3}$
The sum of $M$ over all test cases does not exceed $100$
Subtask #2 (40 points):
$1β€ M β€ 10^{3}$
$1β€ N β€ 10^{3}$
$0β€ B_{i} β€ 10^{3}$
$0β€ Z_{i} β€ 10^{3}$
The sum of $M$ over all test cases does not exceed $10^{3}$
Subtask #3 (55 points):
Original constraints
----- Sample Input 1 ------
2
2 2
2 3
1 1
2 2
2 4
1 3
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
Test case 1:
$A = [1, 2, 3]$ is one possible array.
- $B = [2, 3]$ is a subsequence of $A$
- $fA = [1, 1]$ is a subsequence of $Z = [1, 1]$.
Test case 2:
It can be shown that no array $A$ can both contain $B = [2, 4]$ as a subsequence and also have $fA$ be a subsequence of $Z = [1, 3]$ | from sys import stdin
input = stdin.readline
def answer():
if n == 1:
return "YES"
for i in range(n - 1):
if b[i] > b[i + 1]:
return "NO"
req, dp = 0, 0
for i in range(m):
diff = b[req + 1] - b[req]
dp |= dp << z[i] & (1 << 100001) - 1
dp |= 1 << z[i] & (1 << 100001) - 1
if dp >> diff & 1:
dp = 0
req += 1
if req == n - 1:
return "YES"
return "NO"
for T in range(int(input())):
n, m = map(int, input().split())
b = list(map(int, input().split()))
z = list(map(int, input().split()))
print(answer()) | ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Read problem statements in [Bengali], [Russian], [Mandarin] and [Vietnamese] as well.
The *functional array* of an array A = [A_{1}, A_{2}, \dots, A_{N}] is the array fA of size N-1, where fA_{i} = A_{i+1} - A_{i} for 1β€ i < N. For example, if A = [2, 3, 9, 11] then fA = [1, 6, 2].
You are given two arrays B = [B_{1}, B_{2}, \dots, B_{N}] and Z = [Z_{1}, Z_{2}, \dots, Z_{M}] of length N and M respectively. Find out whether there exists an array A such that:
B is a [subsequence] of A, and
fA is a subsequence of Z
Print "YES" (without quotes) if such an array A exists, and "NO" otherwise.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- Each test case contains three lines of input.
- The first line contains two integers N and M - the lengths of B and Z respectively.
- The second line contains N space-separated integers B_{1}, B_{2}, \dots, B_{N}; the elements of B.
- The third line contains M space-separated integers Z_{1}, Z_{2}, \dots, Z_{M}; the elements of Z.
------ Output Format ------
For each test case, output a single line containing one word - either "YES" or "NO". You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ M β€ 10^{5}$
$0 β€ B_{i} β€ 10^{5}$
$0 β€ Z_{i} β€ 10^{5}$
- The sum of $N$ over all test cases does not exceed $10^{5}$
- The sum of $M$ over all test cases does not exceed $10^{5}$
------ subtasks ------
Subtask #1 (5 points):
$1β€ M β€ 10$
$1β€ N β€ 10^{3}$
$0β€ B_{i} β€ 10^{3}$
$0β€ Z_{i} β€ 10^{3}$
The sum of $M$ over all test cases does not exceed $100$
Subtask #2 (40 points):
$1β€ M β€ 10^{3}$
$1β€ N β€ 10^{3}$
$0β€ B_{i} β€ 10^{3}$
$0β€ Z_{i} β€ 10^{3}$
The sum of $M$ over all test cases does not exceed $10^{3}$
Subtask #3 (55 points):
Original constraints
----- Sample Input 1 ------
2
2 2
2 3
1 1
2 2
2 4
1 3
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
Test case 1:
$A = [1, 2, 3]$ is one possible array.
- $B = [2, 3]$ is a subsequence of $A$
- $fA = [1, 1]$ is a subsequence of $Z = [1, 1]$.
Test case 2:
It can be shown that no array $A$ can both contain $B = [2, 4]$ as a subsequence and also have $fA$ be a subsequence of $Z = [1, 3]$ | for _ in range(int(input())):
N, M = map(int, input().strip().split(" "))
B = list(map(int, input().strip().split(" ")))
Z = list(map(int, input().strip().split(" ")))
yes = M >= N - 1
for i in range(1, N):
yes = yes and B[i] >= B[i - 1]
ind = 0
for i in range(N - 1):
if not yes:
break
R = B[i + 1] - B[i]
if R == 0:
while ind < M and Z[ind] != 0:
ind += 1
if ind == M:
yes = False
ind += 1
else:
dp = 1
mod = (1 << R + 1) - 1
while ind < M and dp >> R & 1 == 0:
if Z[ind] <= R:
dp |= dp << Z[ind] & mod
ind += 1
if dp >> R & 1 == 0:
yes = False
print("YES" if yes else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Parsa has a humongous tree on $n$ vertices.
On each vertex $v$ he has written two integers $l_v$ and $r_v$.
To make Parsa's tree look even more majestic, Nima wants to assign a number $a_v$ ($l_v \le a_v \le r_v$) to each vertex $v$ such that the beauty of Parsa's tree is maximized.
Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of $|a_u - a_v|$ over all edges $(u, v)$ of the tree.
Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree.
-----Input-----
The first line contains an integer $t$ $(1\le t\le 250)$ β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ $(2\le n\le 10^5)$ β the number of vertices in Parsa's tree.
The $i$-th of the following $n$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i \le r_i \le 10^9)$.
Each of the next $n-1$ lines contains two integers $u$ and $v$ $(1 \le u , v \le n, u\neq v)$ meaning that there is an edge between the vertices $u$ and $v$ in Parsa's tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print the maximum possible beauty for Parsa's tree.
-----Examples-----
Input
3
2
1 6
3 8
1 2
3
1 3
4 6
7 9
1 2
2 3
6
3 14
12 20
12 19
2 12
10 17
3 17
3 2
6 5
1 5
2 6
4 6
Output
7
8
62
-----Note-----
The trees in the example:
In the first test case, one possible assignment is $a = \{1, 8\}$ which results in $|1 - 8| = 7$.
In the second test case, one of the possible assignments is $a = \{1, 5, 9\}$ which results in a beauty of $|1 - 5| + |5 - 9| = 8$ | import sys
input = sys.stdin.readline
def dfs(i, p, l0, l1, dp0, dp1, adj):
for j in adj[i]:
if j != p:
dfs(j, i, l0, l1, dp0, dp1, adj)
dp0[p] += max(dp0[i] + abs(l0[p] - l0[i]), dp1[i] + abs(l0[p] - l1[i]))
dp1[p] += max(dp0[i] + abs(l1[p] - l0[i]), dp1[i] + abs(l1[p] - l1[i]))
def solve():
n = int(input())
l0, l1 = [0] * (n + 1), [0] * (n + 1)
for i in range(1, n + 1):
l0[i], l1[i] = map(int, input().split())
adj = [[] for i in range(n + 1)]
for i in range(n - 1):
a, b = map(int, input().split())
adj[a].append(b)
adj[b].append(a)
dp1, dp0 = [0] * (n + 1), [0] * (n + 1)
dfs(1, 0, l0, l1, dp0, dp1, adj)
print(max(dp0[1], dp1[1]))
for _ in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Parsa has a humongous tree on $n$ vertices.
On each vertex $v$ he has written two integers $l_v$ and $r_v$.
To make Parsa's tree look even more majestic, Nima wants to assign a number $a_v$ ($l_v \le a_v \le r_v$) to each vertex $v$ such that the beauty of Parsa's tree is maximized.
Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of $|a_u - a_v|$ over all edges $(u, v)$ of the tree.
Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree.
-----Input-----
The first line contains an integer $t$ $(1\le t\le 250)$ β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ $(2\le n\le 10^5)$ β the number of vertices in Parsa's tree.
The $i$-th of the following $n$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i \le r_i \le 10^9)$.
Each of the next $n-1$ lines contains two integers $u$ and $v$ $(1 \le u , v \le n, u\neq v)$ meaning that there is an edge between the vertices $u$ and $v$ in Parsa's tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print the maximum possible beauty for Parsa's tree.
-----Examples-----
Input
3
2
1 6
3 8
1 2
3
1 3
4 6
7 9
1 2
2 3
6
3 14
12 20
12 19
2 12
10 17
3 17
3 2
6 5
1 5
2 6
4 6
Output
7
8
62
-----Note-----
The trees in the example:
In the first test case, one possible assignment is $a = \{1, 8\}$ which results in $|1 - 8| = 7$.
In the second test case, one of the possible assignments is $a = \{1, 5, 9\}$ which results in a beauty of $|1 - 5| + |5 - 9| = 8$ | import sys
input = sys.stdin.buffer.readline
for t in range(int(input())):
N = int(input())
X = [list(map(int, input().split())) for i in range(N)]
G = [[] for i in range(N)]
for i in range(N - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
G[a].append(b)
G[b].append(a)
r = 0
DP = [[0, 0] for i in range(N)]
Q = [(r, 0, -1)]
while len(Q):
w = Q[-1]
v = w[0]
del Q[-1]
if w[1]:
for t in range(2):
for i in range(len(G[v])):
if w[2] == G[v][i]:
continue
m = 0
for j in range(2):
m = max(m, DP[G[v][i]][j] + abs(X[G[v][i]][j] - X[v][t]))
DP[v][t] += m
continue
Q.append((v, 1, w[2]))
for i in range(len(G[v])):
if w[2] != G[v][i]:
Q.append((G[v][i], 0, v))
print(max(DP[0])) | IMPORT ASSIGN VAR 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 VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Parsa has a humongous tree on $n$ vertices.
On each vertex $v$ he has written two integers $l_v$ and $r_v$.
To make Parsa's tree look even more majestic, Nima wants to assign a number $a_v$ ($l_v \le a_v \le r_v$) to each vertex $v$ such that the beauty of Parsa's tree is maximized.
Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of $|a_u - a_v|$ over all edges $(u, v)$ of the tree.
Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree.
-----Input-----
The first line contains an integer $t$ $(1\le t\le 250)$ β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ $(2\le n\le 10^5)$ β the number of vertices in Parsa's tree.
The $i$-th of the following $n$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i \le r_i \le 10^9)$.
Each of the next $n-1$ lines contains two integers $u$ and $v$ $(1 \le u , v \le n, u\neq v)$ meaning that there is an edge between the vertices $u$ and $v$ in Parsa's tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print the maximum possible beauty for Parsa's tree.
-----Examples-----
Input
3
2
1 6
3 8
1 2
3
1 3
4 6
7 9
1 2
2 3
6
3 14
12 20
12 19
2 12
10 17
3 17
3 2
6 5
1 5
2 6
4 6
Output
7
8
62
-----Note-----
The trees in the example:
In the first test case, one possible assignment is $a = \{1, 8\}$ which results in $|1 - 8| = 7$.
In the second test case, one of the possible assignments is $a = \{1, 5, 9\}$ which results in a beauty of $|1 - 5| + |5 - 9| = 8$ | import sys
input = sys.stdin.readline
def dfs(u, p, dp0, dp1, adj, lB, rB):
for v in adj[u]:
if v != p:
dfs(v, u, dp0, dp1, adj, lB, rB)
dp0[p] += max(dp0[u] + abs(lB[p] - lB[u]), dp1[u] + abs(lB[p] - rB[u]))
dp1[p] += max(dp0[u] + abs(rB[p] - lB[u]), dp1[u] + abs(rB[p] - rB[u]))
def solve():
n = int(input())
lB = [(0) for x in range(n + 1)]
rB = [(0) for x in range(n + 1)]
dp0 = [(0) for x in range(n + 1)]
dp1 = [(0) for x in range(n + 1)]
for i in range(1, n + 1):
lB[i], rB[i] = map(int, input().split())
adj = [[] for x in range(n + 1)]
for k in range(n - 1):
u, v = map(int, input().split())
adj[u].append(v)
adj[v].append(u)
dfs(1, 0, dp0, dp1, adj, lB, rB)
print(max(dp0[1], dp1[1]))
for _ in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Parsa has a humongous tree on $n$ vertices.
On each vertex $v$ he has written two integers $l_v$ and $r_v$.
To make Parsa's tree look even more majestic, Nima wants to assign a number $a_v$ ($l_v \le a_v \le r_v$) to each vertex $v$ such that the beauty of Parsa's tree is maximized.
Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of $|a_u - a_v|$ over all edges $(u, v)$ of the tree.
Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree.
-----Input-----
The first line contains an integer $t$ $(1\le t\le 250)$ β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ $(2\le n\le 10^5)$ β the number of vertices in Parsa's tree.
The $i$-th of the following $n$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i \le r_i \le 10^9)$.
Each of the next $n-1$ lines contains two integers $u$ and $v$ $(1 \le u , v \le n, u\neq v)$ meaning that there is an edge between the vertices $u$ and $v$ in Parsa's tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print the maximum possible beauty for Parsa's tree.
-----Examples-----
Input
3
2
1 6
3 8
1 2
3
1 3
4 6
7 9
1 2
2 3
6
3 14
12 20
12 19
2 12
10 17
3 17
3 2
6 5
1 5
2 6
4 6
Output
7
8
62
-----Note-----
The trees in the example:
In the first test case, one possible assignment is $a = \{1, 8\}$ which results in $|1 - 8| = 7$.
In the second test case, one of the possible assignments is $a = \{1, 5, 9\}$ which results in a beauty of $|1 - 5| + |5 - 9| = 8$ | import sys
input = sys.stdin.buffer.readline
def main():
t = int(input())
INF = float("inf")
for _ in range(t):
n = int(input())
L = []
R = []
for i in range(n):
l, r = map(int, input().split())
L.append(l)
R.append(r)
G = [[] for _ in range(n)]
for i in range(n - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
G[a].append(b)
G[b].append(a)
root = 0
par = [-1] * n
stack = []
stack.append(~0)
stack.append(0)
dp = [[0, 0] for _ in range(n)]
while stack:
v = stack.pop()
if v >= 0:
for u in G[v]:
if u == par[v]:
continue
par[u] = v
stack.append(~u)
stack.append(u)
else:
u = ~v
v = par[u]
if v == -1:
continue
zero = max(dp[u][0] + abs(L[v] - L[u]), dp[u][1] + abs(L[v] - R[u]))
one = max(dp[u][0] + abs(R[v] - L[u]), dp[u][1] + abs(R[v] - R[u]))
dp[v][0] += zero
dp[v][1] += one
ans = max(dp[0])
print(ans)
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Parsa has a humongous tree on $n$ vertices.
On each vertex $v$ he has written two integers $l_v$ and $r_v$.
To make Parsa's tree look even more majestic, Nima wants to assign a number $a_v$ ($l_v \le a_v \le r_v$) to each vertex $v$ such that the beauty of Parsa's tree is maximized.
Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of $|a_u - a_v|$ over all edges $(u, v)$ of the tree.
Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree.
-----Input-----
The first line contains an integer $t$ $(1\le t\le 250)$ β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ $(2\le n\le 10^5)$ β the number of vertices in Parsa's tree.
The $i$-th of the following $n$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i \le r_i \le 10^9)$.
Each of the next $n-1$ lines contains two integers $u$ and $v$ $(1 \le u , v \le n, u\neq v)$ meaning that there is an edge between the vertices $u$ and $v$ in Parsa's tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print the maximum possible beauty for Parsa's tree.
-----Examples-----
Input
3
2
1 6
3 8
1 2
3
1 3
4 6
7 9
1 2
2 3
6
3 14
12 20
12 19
2 12
10 17
3 17
3 2
6 5
1 5
2 6
4 6
Output
7
8
62
-----Note-----
The trees in the example:
In the first test case, one possible assignment is $a = \{1, 8\}$ which results in $|1 - 8| = 7$.
In the second test case, one of the possible assignments is $a = \{1, 5, 9\}$ which results in a beauty of $|1 - 5| + |5 - 9| = 8$ | import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
n = int(input())
a = [0] + [list(map(int, input().split())) for i in range(n)]
adj = [[] for i in range(n + 1)]
for i in range(n - 1):
u, v = map(int, input().split())
adj[u].append(v)
adj[v].append(u)
dp = [([0.0] * 2) for i in range(n + 1)]
s = [1]
vis = [0] * (n + 1)
done = [0] * (n + 1)
while s:
c = s[-1]
if not vis[c]:
vis[c] = 1
for ne in adj[c]:
if not vis[ne]:
s.append(ne)
else:
for ne in adj[c]:
if done[ne]:
dp[c][1] += max(
dp[ne][0] + abs(a[c][1] - a[ne][0]),
dp[ne][1] + abs(a[c][1] - a[ne][1]),
)
dp[c][0] += max(
dp[ne][0] + abs(a[c][0] - a[ne][0]),
dp[ne][1] + abs(a[c][0] - a[ne][1]),
)
done[c] = 1
s.pop()
print(int(max(dp[1]))) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
n, a, b = list(map(int, input().split()))
s = [int(i) for i in input()]
temp, ar, tot = [0] * 3
tot += a * n
for i in s:
if not i:
if ar:
temp += 1
ar = 0
else:
temp += 1
ar = 1
tot += temp * b if temp else 0
if sum(s):
tot += a
curr = i = 0
while i < len(s):
if s[i]:
curr = 1
elif curr:
temp = i
while temp < len(s) and not s[temp]:
temp += 1
if temp == len(s):
tot += a if sum(s) else 0
elif (temp - i - 1) * b <= 2 * a:
tot += (temp - i - 1) * b
elif 2 * a < (temp - i - 1) * b:
tot += 2 * a
i = temp
i += 1
print(tot + (n + 1) * b) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER NUMBER VAR BIN_OP VAR VAR FOR VAR VAR IF VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | q = int(input())
for query in range(q):
n, a, b = map(int, input().split())
s = input()
cost, up = a * n + (n + 1) * b, False
i = 0
while i < n - 1:
if up:
cost += b
elif s[i] == "0" and s[i + 1] == "1" and not up:
cost += a
up = True
if up and s[i] == "0" and s[i + 1] == "0":
tempc = i
while i < n and s[i] == "0":
i += 1
if i == n:
cost += a
up = False
i -= 1
elif (i - tempc - 1) * b > 2 * a:
cost += a
up = False
i -= 1
else:
cost += (i - tempc - 1) * b
else:
i += 1
if s[n - 2] == "1":
cost += b
if up:
cost += a
print(cost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for it in range(t):
n, a, b = map(int, input().split())
s = input()
dp = [([int(1e18)] * 2) for i in range(n + 1)]
dp[0][0] = b
for i in range(n):
if s[i] == "1":
dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + a + 2 * b)
else:
dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + a + b)
dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + 2 * (a + b))
dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + a + 2 * b)
dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + 2 * a + b)
print(dp[n][0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
while t > 0:
t -= 1
n, a, b = map(int, input().split())
s = input()
i = 0
ans = 0
while i < n:
j = i
while j < n:
if s[j] != s[i]:
break
j += 1
if s[i] == "0":
k = j - i
c1 = (k + 2) * a + (k + 1) * b
c2 = 2 * b * k + k * a
if j == n:
c1 -= a + b
c2 -= b - a
if i == 0:
c1 -= a - b
c2 += 2 * b
if i == 0 or j == n:
ans += c1
else:
ans += min(c1, c2)
else:
ans += (j - i) * a + 2 * (j - i) * b
i = j
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | m = int(input())
for i in range(m):
t = 0
n, a, b = map(int, input().split())
s = list(input())
for j in range(n):
s[j] = int(s[j])
k = s[0]
p = 1
d = []
for j in range(1, n):
if s[j] == s[j - 1]:
p += 1
else:
d.append(p)
p = 1
d.append(p)
for j in range(len(d)):
if k == 0:
if j == 0:
t += a + d[j] * a + 1 * b * d[j]
elif j == len(d) - 1:
t += a + d[j] * a + 1 * b * d[j] + 2 * b
elif (
2 * a + d[j] * a + 1 * b * (d[j] - 1) + 2 * b <= 2 * b * d[j] + d[j] * a
):
t += 2 * a + d[j] * a + 1 * b * (d[j] - 1) + 2 * b
else:
t += 2 * b * d[j] + d[j] * a
k = 1
else:
t += 2 * b * d[j] + d[j] * a
k = 0
if j == len(d) - 1:
t += 2 * b
if len(d) == 1 and s[0] == 0:
t = d[0] * a + (d[0] + 1) * b
elif len(d) == 1 and s[0] == 1:
t = d[0] * a + (d[0] + 1) * 2 * b
print(t) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR IF BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def minCost(n, road, a, b):
start = end = price = 0
price += b
while end != n:
if end < n - 1 and road[end + 1] == 0:
end += 1
else:
length = end - start
if start == 0:
if end == n - 1:
return n * a + n * b + b
else:
price += (a + b) * (length + 2)
elif end == n - 1:
price += (a + b) * (length + 2)
elif length:
if b * (length - 1) < 2 * a:
price += (a + 2 * b) * (length + 1)
else:
price += (a + b) * (length + 3)
else:
price += a + 2 * b
start = end = end + 1
return price
r = int(input(""))
for i in range(0, r):
road_size, t_price, s_price = [int(x) for x in str(input("")).split(" ")]
road = [int(x) for x in str(input(""))]
print(minCost(road_size, road, t_price, s_price)) | FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR VAR WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
t = int(input())
input = sys.stdin.readline
maxi = 9999999999999999999999999999999999999999999999999999999
while t > 0:
t -= 1
n, aa, b = map(int, input().split())
a = list(input())
for i in range(n):
a[i] = int(a[i])
dp = [[0, 0] for i in range(n + 1)]
if a[1] == 0:
dp[1][0] = aa + 2 * b
dp[1][1] = 2 * aa + 3 * b
elif a[1] == 1:
dp[1][0] = maxi
dp[1][1] = 2 * aa + 3 * b
for i in range(1, n - 1):
if a[i + 1] == 0:
dp[i + 1][0] = min(dp[i][0] + aa + b, dp[i][1] + 2 * aa + b)
dp[i + 1][1] = min(dp[i][0] + 2 * aa + 2 * b, dp[i][1] + aa + 2 * b)
else:
dp[i + 1][0] = maxi
dp[i + 1][1] = min(dp[i][0] + 2 * aa + 2 * b, dp[i][1] + aa + 2 * b)
if a[i] == 1:
dp[i + 1][0] = maxi
print(min(dp[n - 1][0] + aa + b, dp[n - 1][1] + 2 * aa + b)) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for i in range(t):
n, x, y = map(int, input().split())
a = input()
if a == "0" * n:
print((n + 1) * y + n * x)
else:
r1 = 0
r2 = 0
s = 1
b = []
S = 0
k = 0
for i in range(1, n):
if a[i] != a[i - 1]:
b.append(s)
s = 1
else:
s += 1
if a[i] == "0":
k += 1
b.append(s)
s = 0
for i in range(1, len(b)):
if i % 2 == 1:
S += b[i] * x + (b[i] + 1) * y * 2
for i in range(0, len(b)):
if i % 2 == 0:
if i == 0 or i == len(b) - 1:
S += b[i] * y + b[i] * x + x
else:
f1 = b[i] * x + (b[i] - 1) * 2 * y
f2 = (b[i] + 2) * x + (b[i] - 1) * y
S += min(f1, f2)
print(S) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | from itertools import groupby
total = int(input())
for i in range(total):
n, a, b = [int(x) for x in input().split()]
road = input()
sroad = ["".join(group) for key, group in groupby(road)]
l = len(sroad)
if l == 1:
print(n * a + (n + 1) * b)
else:
cnt = 0
total_cost = 0
for chunk in sroad:
lc = len(chunk)
if "0" in chunk:
if cnt == 0:
total_cost += lc * b + (lc + 1) * a
elif cnt == l - 1:
total_cost += (lc + 2) * b + (lc + 1) * a
else:
total_cost += min(a * lc + 2 * b * lc, (lc + 2) * a + b * (lc + 1))
else:
total_cost += 2 * b * lc + a * lc
cnt += 1
print(total_cost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for _ in range(t):
n, a, b = map(int, input().split())
s = input()
res = 0
i = 0
if n == 2:
print(3 * b + 2 * a)
continue
if s == "0" * n:
print((n + 1) * b + n * a)
continue
while i < len(s):
if s[i] == "0":
if s[i + 1] == "0":
res += b + a
else:
res += 2 * a + b
i += 1
elif s[i + 1] == "1":
res += 2 * b + a
i += 1
else:
res += 2 * b + a
for j in range(i + 1, len(s)):
if s[j] == "1":
if j - i > 2:
if 2 * a < (j - i - 2) * b:
res += 2 * a + (j - i - 1) * a + (j - i) * b
else:
res += (j - i - 1) * a + 2 * (j - i - 1) * b
i = j
break
else:
res += 2 * b + a
i = j
break
else:
res += (len(s) - i) * (a + b) + b
break
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | mod = int(1000000000.0) + 7
x = float("inf")
def ip(mode):
if mode == 0:
return int(input())
if mode == 1:
return list(map(int, input().split()))
if mode == 2:
return input()
if mode == 3:
return list(map(str, input().split()))
if mode == 4:
return map(int, input().split())
if mode == 5:
return map(str, input().split())
for _ in range(int(input())):
n, a, b = ip(4)
dp = [[0, 0, 0] for i in range(n + 1)]
s = ip(2)
dp[0][1] = b
dp[0][2] = x
for i in range(1, n):
dp[i][2] = min(dp[i - 1][1] + 2 * a + 2 * b, dp[i - 1][2] + a + 2 * b)
if s[i] == "0" and s[i - 1] == "0":
dp[i][1] = min(dp[i - 1][1] + a + b, dp[i - 1][2] + b + 2 * a)
else:
dp[i][1] = x
dp[n][1] = min(dp[n - 1][1] + a + b, dp[n - 1][2] + b + 2 * a)
print(dp[n][1]) | ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def mi():
return list(map(int, input().split()))
for _ in range(int(input())):
n, a, b = mi()
s = list(map(int, input()))
ans = (a + b) * n + b
if 1 not in s:
print(ans)
continue
i1 = s.index(1)
i2 = n - 1 - s[::-1].index(1)
ans += 2 * a + (i2 - i1 + 2) * b
i = i1
while i < i2:
enter = False
while i < i2 and s[i] == 1:
enter = True
i += 1
start = i
while i < i2 and s[i] == 0:
enter = True
i += 1
if i < n and s[i] == 1 and s[i - 1] == 0:
enter = True
if 2 * a - b * (i - start - 1) < 0:
ans += 2 * a - b * (i - start - 1)
if not enter:
break
print(ans) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
n, pipe_price, pillar_price = map(int, input().split())
s = input()
ans = pillar_price * (n + 1) + pipe_price * n
max_space = int(pipe_price * 2 / pillar_price)
i = 0
while i < n and s[i] == "0":
i += 1
if i == n:
print(ans)
continue
j = n - 1
while s[j] == "0":
j -= 1
s = s[i : j + 1]
ans += pipe_price * 2 + (len(s) + 1) * pillar_price
i = 0
while i < len(s):
seq = 0
while s[i] == "0":
seq += 1
i += 1
i += 1
if seq - 1 > max_space:
ans -= pillar_price * (seq - 1)
ans += pipe_price * 2
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for i in range(int(input())):
n, a, b = input().split(" ")
n = int(n)
a = int(a)
b = int(b)
patterns = list(input())
groups = []
bit = "0"
count = 0
for i in patterns:
if i == bit:
count += 1
else:
groups.append(count)
count = 1
bit = i
groups.append(count)
if len(groups) == 1:
print(n * a + (n + 1) * b)
else:
zeros = groups[0::2]
ones = groups[1::2]
cost = (zeros[0] + zeros[-1] + 2) * (a + b)
cost += sum(ones) * (2 * b + a)
for i in range(1, len(zeros) - 1):
cost1 = zeros[i] * (a + 2 * b)
cost2 = zeros[i] * (a + b) + 2 * a + b
if cost1 < cost2:
cost += cost1
else:
cost += cost2
print(cost) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | T = int(input())
for _ in range(T):
n, a, b = map(int, input().split())
s = [int(x) for x in input()]
ans = (n + 1) * b
k = 0
fp = True
for i in range(len(s)):
if s[i] == 0:
k += 1
else:
if fp:
fp = False
ans += (k + 2) * a + 2 * b
elif k == 0:
ans += a + b
else:
ans += min((k + 3) * a + 2 * b, (k + 1) * (a + b))
k = 0
if fp:
ans += k * a
else:
ans += (k + 1) * a
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | T = int(input())
for _ in range(T):
n, a, b = [int(i) for i in input().split()]
road = input()
num_one = 0
zero_seg = [(0) for i in range(n)]
num_zer = 0
len_zer = 0
acc_down = True
for i in range(n):
if road[i] == "1":
num_one += 1
if acc_down:
acc_down = False
zero_seg[num_zer] = len_zer
len_zer = 0
num_zer += 1
else:
len_zer += 1
acc_down = True
zero_seg[num_zer] = len_zer
num_zer += 1
pillars = num_one + num_zer - 1
if num_one >= 1:
zikzak = 2
for i in range(1, num_zer - 1):
if (zero_seg[i] - 1) * b > 2 * a:
zikzak += 2
else:
pillars += zero_seg[i] - 1
res = (n + 1) * b + n * a
res += zikzak * a
res += pillars * b
print(res)
else:
print((n + 1) * b + n * a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
n, a, b = map(int, input().split())
s = input().strip()
ans = (n + 1) * b + n * a
if "1" in s:
ans += 2 * a
s = s[s.find("1") : s.rfind("1") + 1]
t = [0] * (len(s) + 1)
for i in range(len(s)):
x = int(s[i])
t[i] = max(t[i], x)
t[i + 1] = max(t[i + 1], x)
i = 0
while i < len(t):
j = i + 1
while j < len(t) and t[i] == t[j]:
j += 1
if t[i]:
ans += b * (j - i)
else:
ans += min(b * (j - i), 2 * a)
i = j
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF STRING VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for j in range(t):
n, a, b = map(int, input().split())
s = input()
k = 0
z = 0
for i in range(n):
if s[i] == "0":
z += 1
else:
if z == 1:
if i == z:
k += 3 * b + 2 * a
else:
k += a + 2 * b
elif z > 1:
if i == z:
alter_1 = 3 * b + 2 * a + (z - 1) * (a + 2 * b)
alter_2 = 2 * b + a + (z - 2) * (a + b) + 2 * a + 2 * b
else:
alter_1 = z * (a + 2 * b)
alter_2 = 2 * a + b + (z - 2) * (a + b) + 2 * a + 2 * b
k += min(alter_1, alter_2)
k += a + 2 * b
z = 0
if z == n:
k = b + z * (a + b)
else:
alter_1 = (z - 1) * (a + 2 * b) + 2 * a + b
alter_2 = 2 * a + b + (z - 1) * (a + b)
k += min(alter_1, alter_2)
print(k) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | from itertools import groupby
T = int(input())
for _ in range(T):
n, a, b = tuple(map(int, input().split()))
s = input()
G = [(k, len(list(g)), False) for k, g in groupby(s)]
G[-1] = G[-1][0], G[-1][1], True
q = 0
state = 0
for k, L, last in G:
if state == 0 and k == "0":
if last:
q += L * a
q += (L + 1) * b
else:
q += (L + 1) * a
q += L * b
elif state == 0 and k == "1":
q += L * a
q += L * 2 * b
state = 1
elif state == 1 and k == "0":
if last:
q += (L + 1) * a
q += (L + 2) * b
else:
q1 = (L + 2) * a + (L + 1) * b
q2 = L * a + 2 * L * b
q += min(q1, q2)
state = 0
print(q) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER VAR STRING IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR STRING VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR STRING IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for x in range(t):
n, a, b = map(int, input().split())
l = list(input())
s = 0
i = 0
while i < n:
if i == 0:
while i < n and l[i] != "1":
i += 1
s += a + b
s += a
if i == n:
s -= a
break
if l[i] == "0":
c = 0
while i < n and l[i] != "1":
c += 1
i += 1
if i == n:
s += (c + 1) * b + (c + 1) * a
break
elif c > 1:
s1 = min((c + 1) * b + (c + 2) * a, c * 2 * b + c * a)
s += s1
else:
s += 2 * b + a
if l[i] == "1":
while l[i] != "0":
s += 2 * b + a
i += 1
if i == n - 1:
s += 2 * b + 2 * a
break
print(s + b) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR STRING WHILE VAR VAR STRING VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | T = int(input())
for _ in range(T):
n, a, b = list(map(int, input().split()))
s = input()
ans = n * a + (n + 1) * b
li = s.find("1")
if li >= 0:
ri = s.rfind("1")
ans += a + a + (ri - li + 2) * b
lens = []
for i in range(li + 1, ri):
if s[i] == "0":
if s[i - 1] == "1":
lens.append(0)
lens[-1] += 1
for l in lens:
sm = a + a - (l - 1) * b
if l > 1 and sm < 0:
ans += sm
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def get_road_chunk(cur_pos):
r = cur_pos
while r + 1 < len(s) and s[r + 1] == "0":
r += 1
return r - cur_pos + 1
T = int(input())
for i in range(T):
n, a, b = map(int, input().split())
s = input()
k = 0
p = b
cur_level = 1
while k < len(s):
if s[k] == "1":
p += a + 2 * b
k += 1
elif get_road_chunk(k) == 1:
if cur_level == 1:
p += 2 * a + 2 * b
cur_level = 2
elif k == len(s) - 1:
p += a * 2 + b
else:
p += a + 2 * b
k += 1
else:
l = get_road_chunk(k)
if l + k != len(s):
if cur_level == 2:
if a * (l + 2) + b * (l + 1) < a * l + b * 2 * l:
p += a * (l + 2) + b * (l + 1)
else:
p += a * l + b * 2 * l
else:
p += a * (l + 1) + b * (l + 1)
cur_level = 2
elif cur_level == 2:
p += a * (l + 1) + b * l
else:
p += a * l + b * l
k = k + l
print(p) | FUNC_DEF ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | inf = 10000000000000000000000000000000000000000000000000000000000000000
def solve(n, a, b, s):
dp = [[]] * 2
for i in range(2):
dp[i] = [0] * n
dp[0][0] = (n + 1) * (a + b) - a
dp[1][0] = inf
for i in range(1, n):
if s[i] == "1":
dp[1][i] = min(dp[1][i - 1] + b, dp[0][i - 1] + a + 2 * b)
dp[0][i] = inf
else:
dp[0][i] = min(dp[0][i - 1], dp[1][i - 1] + a)
dp[1][i] = min(dp[1][i - 1] + b, dp[0][i - 1] + a + 2 * b)
return dp[0][n - 1]
for i in range(int(input())):
n, a, b = map(int, input().split())
s = input()
print(solve(n, a, b, s)) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for i in range(t):
n, a, b = map(int, input().split())
s = input()
arr = [[(10**31) for i in range(2)] for j in range(n + 1)]
arr[0][0] = b
for i in range(1, n + 1):
if s[i - 1] == "0":
arr[i][1] = min(arr[i - 1][1] + 2 * b + a, arr[i - 1][0] + 2 * b + 2 * a)
arr[i][0] = min(arr[i - 1][1] + 2 * a + b, arr[i - 1][0] + a + b)
else:
arr[i][1] = arr[i - 1][1] + 2 * b + a
print(arr[n][0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for j in range(t):
n, a, b = map(int, input().split())
s = str(input())
string = [s[i] for i in range(n)]
for i in range(1, n - 1):
if string[i] == "0":
if string[i - 1] == "1" and string[i + 1] == "1":
string[i] = "1"
del s
zero = []
one = []
positions_of_one = []
ones = 0
k_zero, k_one = 0, 0
for i in range(n):
if string[i] == "0" or i == n - 1:
k_zero += 1
one.append(k_one)
k_one = 0
if string[i] == "1" or i == n - 1:
k_one += 1
zero.append(k_zero)
k_zero = 0
if string[i] == "1":
ones += 1
positions_of_one.append(i)
if ones == 0:
print(int(b * (n + 1) + a * n))
continue
zero.pop(len(zero) - 1)
zero.pop(0)
s = (
positions_of_one[0] + (n - positions_of_one[len(positions_of_one) - 1] - 1)
) * b + (
positions_of_one[0] + (n - positions_of_one[len(positions_of_one) - 1] + 1)
) * a
for i in range(len(one)):
if one[i] != 0:
s += (one[i] + 1) * 2 * b + a * one[i]
for i in range(len(zero)):
if zero[i] != 0:
s += min(
a * zero[i] + (zero[i] - 1) * 2 * b,
(zero[i] + 2) * a + (zero[i] - 1) * b,
)
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | q = int(input())
for query in range(q):
n, a, b = map(int, input().split())
s = input()
pod = []
count = 1
for i in range(1, n):
if s[i] != s[i - 1]:
pod.append(count)
count = 1
else:
count += 1
pod.append(count)
wyn = n * a + 2 * n * b + 2 * b
for i in range(len(pod)):
if i % 2 == 0:
if i == 0 or i == len(pod) - 1:
wyn -= b * pod[i] - a
else:
wyn -= max(0, b * (pod[i] - 1) - 2 * a)
if len(pod) == 1:
print((n + 1) * b + n * a)
else:
print(wyn) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | x = int(input())
Ans = []
for i in range(x):
n, a, b = map(int, input().split())
s = input()
index_of_ferst = -1
for j in range(len(s)):
if s[j] == "1" and index_of_ferst == -1:
index_of_ferst = j
index_of_end = -1
for j in range(len(s) - 1, -1, -1):
if s[j] == "1" and index_of_end == -1:
index_of_end = j
if index_of_ferst != -1:
S = s[index_of_ferst : index_of_end + 1]
z = (n + 2) * a + (n + 1) * b + (index_of_end - index_of_ferst + 2) * b
K = []
ox = 0
for j in range(len(S)):
if S[j] == "0":
ox += 1
elif ox != 0:
K.append(ox)
ox = 0
for k in K:
if b * (k - 1) > 2 * a:
z -= b * (k - 1) - 2 * a
Ans.append(z)
else:
Ans.append(n * a + (n + 1) * b)
for ans in Ans:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | test = int(input())
for _ in range(test):
n, a, b = map(int, input().split())
s = input()
ans = a * n + b * (n + 1)
for i in range(n):
if s[n - i - 1] == "1":
s = s[: n - i]
break
for i in range(len(s)):
if s[i] == "1":
s = s[i:]
break
if s.count("1") == 0:
print(ans)
continue
dp = []
c = 1
for i in range(len(s) - 1):
if s[i + 1] == s[i]:
c += 1
else:
dp.append(c)
c = 1
dp.append(c)
for i in dp[::2]:
ans += (i + 1) * b
ans += len(dp[::2]) * a * 2
for i in dp[1::2]:
if (i - 1) * b < 2 * a:
ans += (i - 1) * b
ans -= 2 * a
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def gns():
return list(map(int, input().split()))
t = int(input())
for ti in range(t):
n, a, b = gns()
s = input()
ans = s.split("1")
ans = [len(x) for x in ans if len(x) > 0]
cst = a * n + 2 * a
cst += (n + 1) * 2 * b - ans[0] * b - ans[-1] * b
if len(ans) == 1:
print((n + 1) * b + n * a)
continue
for i in range(1, len(ans) - 1):
x = ans[i]
cst = min(cst, cst - (x - 1) * b + a * 2)
print(cst) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def main():
T = int(input())
for _ in range(T):
n, a, b = [int(s) for s in input().split()]
s = [int(x) for x in list(input())]
lengths = []
current = 0
length = 0
for segment in s:
if segment == current:
length += 1
else:
lengths.append(length)
length = 1
current = segment
lengths.append(length)
if len(lengths) == 1:
print((n + 1) * b + n * a)
continue
sol = (lengths[0] + 1) * a + lengths[0] * b
sol += (lengths[-1] + 1) * a + (lengths[-1] + 2) * b
current = 1
for length in lengths[1:-1]:
if current == 0 and length > 1:
down = (length + 1) * b + (length + 2) * a
up = length * 2 * b + length * a
sol += min(up, down)
else:
sol += length * 2 * b + length * a
current = (current + 1) % 2
print(sol)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | C = []
for _ in range(int(input())):
n, a, b = map(int, input().split())
pre = None
pos = [None, None]
genc = False
x = 0
cost = (a + b) * n + b
for i, cur in enumerate(input()):
if pre == None:
pre = "0"
continue
if cur == "0":
if pre == "0":
if pos[0] == None and genc:
x += 1
elif genc:
cost += (pos[1] - pos[0] + 2) * b
x = 1
pos[0] = None
pos[1] = None
else:
pos[1] = i - 1
elif pre == "0":
if pos[0] == None:
if genc:
cost += min(b * x, 2 * a)
x = 0
else:
genc = True
pos[0] = i
pre = cur
if genc:
if pos[0] is not None:
cost += 2 * a + (pos[1] - pos[0] + 2) * b
else:
cost += 2 * a
C.append(cost)
for c in C:
print(c) | ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR LIST NONE NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NONE ASSIGN VAR STRING IF VAR STRING IF VAR STRING IF VAR NUMBER NONE VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NONE ASSIGN VAR NUMBER NONE ASSIGN VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING IF VAR NUMBER NONE IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR IF VAR IF VAR NUMBER NONE VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
n, a, b = map(int, input().split())
road = input()
lows = []
i = 0
cost = n * (a + b) + b
while i != n and road[i] == "0":
i += 1
if i == n:
print(cost)
continue
cost += a * 2
while i < n:
start = i
while i < n and road[i] == "1":
i += 1
cost += (i - start + 1) * b
if i == n:
break
start = i
while i < n and road[i] == "0":
i += 1
if i - start - 1 != 0 and i != n:
lows.append(i - start - 1)
for low in lows:
cost += min(2 * a, low * b)
print(cost) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
mod = 1000000007
def get_array():
return list(map(int, sys.stdin.readline().split()))
def get_ints():
return map(int, sys.stdin.readline().split())
def input():
return sys.stdin.readline()
def print_array(a):
print(" ".join(map(str, a)))
def main():
for _ in range(int(input())):
n, a, b = get_ints()
s = input()
cost, cnt, i = n * (a + b) + b, 0, 0
for i in range(n - 1):
if s[i] == "1" or s[i + 1] == "1":
cnt += 1
if cnt > 0:
cost += 2 * a + cnt * b
i = s.index("1")
j = n - 1
while s[j] == "0":
j -= 1
cnt = 0
while i <= j:
if s[i] == "0":
cnt += 1
else:
if cnt > 0:
cost += min((cnt - 1) * b, 2 * a)
cnt = 0
i += 1
print(cost)
main() | IMPORT ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
T = int(sys.stdin.readline())
for t in range(T):
n, a, b = list(map(int, sys.stdin.readline().split()))
road = sys.stdin.readline().rstrip()
intersection = road.count("1")
if intersection > 1:
base = a * (n + 2) + (n + 1) * b
plus = 0
begin = road.index("1")
zerocount = 0
onecount = 0
for i in range(begin, n):
if road[i] == "0":
zerocount += 1
if onecount != 0:
plus += (onecount + 1) * b
onecount = 0
else:
onecount += 1
if zerocount != 0:
plus += min((zerocount - 1) * b, 2 * a)
zerocount = 0
print(base + plus)
else:
answer = a * (n + 2 * intersection) + b * (n + 1 + 2 * intersection)
print(answer) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | MAXN = float("inf")
T = int(input())
for _ in range(T):
n, a, b = map(int, input().split())
s = list(map(int, input()))
dp = [b, MAXN]
for i in range(n):
if s[i]:
dp[0], dp[1] = MAXN, dp[1] + a + 2 * b
else:
dp[0], dp[1] = min(dp[0] + a + b, dp[1] + 2 * a + b), min(
dp[0] + 2 * a + 2 * b, dp[1] + a + 2 * b
)
print(dp[0]) | ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def minCost2(n, road, a, b):
height = 1
cost = b
s = -1
for i in range(0, n):
cost += a + b
if i < n - 1 and (road[i] == 1 or road[i + 1] == 1):
cost += b
if height == 1:
cost += a
height = 2
if road[i] == 1 and s != -1:
m = i - s - 1
if m * b < 2 * a:
cost = cost - 2 * a + m * b
s = -1
elif height == 2:
s = i
cost += a
height = 1
return cost
r = int(input(""))
for i in range(0, r):
road_size, t_price, s_price = [int(x) for x in str(input("")).split(" ")]
road = [int(x) for x in str(input(""))]
print(minCost2(road_size, road, t_price, s_price)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | q = int(input())
R = lambda: list(map(int, input().split()))
for j in range(q):
n, a, b = R()
l = input()
l += "0"
n += 1
s = b
t = 0
if a >= b:
p = 1
else:
p = 0
k = 1
test = 0
for i in range(0, n - 1):
if l[i] == "0" and l[i + 1] == "1" and t == 0:
s += 2 * b + 2 * a
elif l[i] == "0" and l[i + 1] == "1" and t == 1:
if (k - 1) * b - 2 * a <= 0:
s += k * a + 2 * k * b
else:
s += (k + 2) * a + (k + 1) * b
k = 1
elif l[i] == "1" and l[i + 1] == "1":
s += 2 * b + a
elif l[i] == "1" and l[i + 1] == "0":
s += a + 2 * b
t = 1
elif l[i] == "0" and l[i + 1] == "0" and t == 1:
k += 1
test = 1
elif l[i] == "0" and l[i + 1] == "0" and t == 0:
s += a + b
k -= 1
if k > 0 and test == 1:
s += (k + 1) * a + k * b
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | q = int(input())
for _ in range(q):
n, a, b = map(int, input().split())
s = str(input())
dp = [0] * (len(s) + 1)
for i in range(len(s) + 1):
dp[i] = [0] * 2
dp[0][0] = b
dp[0][1] = 1000000000000000.0
for i in range(n):
if s[i] == "0":
dp[i + 1][0] = min(dp[i][0] + a, dp[i][1] + a + a) + b
dp[i + 1][1] = min(dp[i][1] + a, dp[i][0] + a + a) + b + b
else:
dp[i + 1][0] = 1000000000000000.0
dp[i + 1][1] = dp[i][1] + b + b + a
print(dp[n][0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for i in range(t):
n, a, b = map(int, input().split())
s = input()
si = list(s)
for i in range(1, n):
si[i] = s[i]
if s[i - 1] == "1":
si[i] = "1"
si += ["0"]
n += 1
ar = [([0] * 2) for i in range(n)]
ar[0][0] = b
num = 10**20
ar[0][1] = num
for i in range(1, n):
ar[i][1] = min(ar[i - 1][1] + a, ar[i - 1][0] + a * 2) + 2 * b
ar[i][0] = min(ar[i - 1][0] + a, ar[i - 1][1] + a * 2) + b
if si[i] == "1":
ar[i][0] = num
print(ar[-1][0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING VAR LIST STRING VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
def solve(A, n, a, b):
cost = n * a + (n + 1) * b
num_ones = 0
groups_ones = 0
groups_zeros = [0]
on_zeros = True
for i in A:
if i == 0:
if on_zeros:
groups_zeros[-1] += 1
else:
on_zeros = True
groups_zeros.append(1)
else:
if on_zeros:
on_zeros = False
groups_ones += 1
num_ones += 1
cost += (groups_ones + num_ones) * b
for i in range(1, len(groups_zeros) - 1):
z = groups_zeros[i]
cost += min(2 * a, (z - 1) * b)
if num_ones > 0:
cost += 2 * a
return cost
in_file = sys.stdin
t = int(in_file.readline().strip())
for _ in range(t):
n, a, b = map(int, in_file.readline().strip().split())
A = list(map(int, in_file.readline().strip()))
sys.stdout.write(str(solve(A, n, a, b)) + "\n")
sys.stdout.flush() | IMPORT FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
def solve():
n, a, b = map(int, input().split())
dp = [[10**20, 10**20] for i in range(n + 1)]
dp[0][0] = b
s = input()
s += "0"
for i in range(1, n + 1):
if s[i] == "1":
dp[i][1] = min(dp[i - 1][0] + 2 * a + 2 * b, dp[i - 1][1] + a + 2 * b)
elif s[i - 1] != "1":
dp[i][0] = min(dp[i - 1][0] + a + b, dp[i - 1][1] + 2 * a + b)
dp[i][1] = min(dp[i - 1][0] + 2 * a + 2 * b, dp[i - 1][1] + a + 2 * b)
else:
dp[i][1] = dp[i - 1][1] + a + 2 * b
print(dp[n][0])
for i in range(t):
solve() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
def main():
(n,) = [int(x) for x in sys.stdin.readline().strip().split()]
INF = 10**20
for _ in range(n):
n, a, b = [int(x) for x in sys.stdin.readline().strip().split()]
s = sys.stdin.readline().strip()
down = [2 * b + a]
up = [b + 2 * b + 2 * a]
for c in s[1:]:
d = down[-1]
u = up[-1]
if c == "0":
down.append(min(d + a + b, u + 2 * a + b))
up.append(min(d + 2 * a + 2 * b, u + a + 2 * b))
else:
down.append(INF)
up.append(u + a + 2 * b)
print(down[-1])
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | from sys import setrecursionlimit as SRL
from sys import stdin
SRL(10**7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
t = int(rd())
while t:
n, a, b = rrd()
s = str(rd().strip())
ans = (n + 1) * b + a
for i in range(1, n):
if s[i] == "0" and s[i - 1] == "1":
ans += 2 * a + b
elif s[i] == "1" and s[i - 1] == "0":
ans += 2 * a + b
elif s[i] == "1":
ans += b + a
else:
ans += a
if i > 2 and s[i] == "1" and s[i - 1] == "0" and s[i - 2] == "1":
ans += -2 * a
pre = 0
for i in range(1, n):
if s[i] == "0" and s[i - 1] == "1":
pre = i + 1
if s[i] == "1" and s[i - 1] == "0":
tot = max(0, i - pre)
if not tot or not pre:
continue
mo = tot * b - 2 * a
if mo < 0:
ans += mo
print(ans)
t -= 1 | EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR STRING VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | from itertools import groupby
def rint():
return int(input())
def rints():
return list(map(int, input().split()))
t = rint()
for _ in range(t):
n, a, b = rints()
s = input()
cost = n * a + (n + 1) * b
for i in range(n + 1):
left1 = i > 0 and s[i - 1] == "1"
right1 = i < n and s[i] == "1"
if left1 or right1:
cost += b
if 0 < i < n and left1 != right1:
cost += a
g = [sum(1 for _ in v) for k, v in groupby(s) if k == "0"]
for i, gs in enumerate(g[1:-1]):
if (gs - 1) * b < 2 * a:
cost += (gs - 1) * b - 2 * a
print(cost) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR STRING IF VAR VAR VAR VAR IF NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
mod = 10**9 + 7
def ri(flag=0):
if flag == 0:
return [int(i) for i in sys.stdin.readline().split()]
else:
return int(sys.stdin.readline())
for _ in range(ri(1)):
n, pipe_cost, pilar_cost = ri()
take = input()
dp = [
[
999999999999999999999999999999999999999,
999999999999999999999999999999999999999,
]
for i in range(n + 1)
]
cost = 0
temp = 0
pilars = [(1) for i in range(n + 1)]
for i in range(n):
if take[i] == "1":
pilars[i] = 2
pilars[i + 1] = 2
dp[0][0] = pilar_cost
dp[0][1] = 9999999999999999999999999999999999999999999999
for i in range(1, n + 1):
if pilars[i] == 1:
dp[i][1] = 2 * pilar_cost + min(
dp[i - 1][0] + 2 * pipe_cost, dp[i - 1][1] + pipe_cost
)
dp[i][0] = pilar_cost + min(
dp[i - 1][1] + 2 * pipe_cost, dp[i - 1][0] + pipe_cost
)
else:
dp[i][1] = 2 * pilar_cost + min(
dp[i - 1][0] + 2 * pipe_cost, dp[i - 1][1] + pipe_cost
)
print(dp[n][0]) | IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
inf = int(1e18)
for case_num in range(t):
n, a, b = map(int, input().split(" "))
s = input() + "0"
f = [0] * (n + 1)
f[0] = b
g = [0] * (n + 1)
g[0] = inf
for i in range(n):
g[i + 1] = min(f[i] + 2 * a, g[i] + a) + 2 * b
f[i + 1] = (
min(f[i] + a, g[i] + 2 * a) + b if s[i] == "0" and s[i + 1] == "0" else inf
)
print(f[n]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR STRING VAR BIN_OP VAR NUMBER STRING BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
n, a, b = map(int, input().split())
description_of_the_road = [int(i) for i in input()]
cost = 0
if 1 in description_of_the_road:
for start, crossroad in enumerate(description_of_the_road):
if crossroad == 0:
cost += a + b
else:
cost += a
break
end = n - 1
while description_of_the_road[end] != 1:
cost += a + b
end -= 1
else:
cost += a
num_of_zeros = []
flag = 0
for crossroad in range(start, end + 1):
if description_of_the_road[crossroad] == 0 and flag == 0:
flag = 1
num_of_zeros.append(1)
elif description_of_the_road[crossroad] == 0 and flag == 1:
num_of_zeros[-1] += 1
else:
flag = 0
cost -= a
for zeros in num_of_zeros:
if zeros != 1:
if 2 * a < (zeros - 1) * b:
cost += 2 * a + (zeros - 1) * b + (zeros - 1) * a
else:
cost += (zeros - 1) * 2 * b + (zeros - 1) * a
num_of_ones = []
for crossroad in range(start, end + 1):
if description_of_the_road[crossroad] == 1 and flag == 0:
flag = 1
num_of_ones.append(1)
elif description_of_the_road[crossroad] == 1 and flag == 1:
num_of_ones[-1] += 1
else:
flag = 0
for ones in num_of_ones:
cost += (ones + 1) * 2 * b + (ones + 1) * a
else:
cost = a * n + b * (n + 1)
print(cost) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR VAR IF VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for d in range(t):
n, a, b = map(int, input().split())
s = [int(p) for p in list(input())]
c = b
i = 0
z = True
while i < n:
j = i
if z:
while j + 1 < n and s[j + 1] == 0:
j += 1
l = j - i + 1
if l == n:
c += l * a + l * b
break
elif i == 0:
c += (l + 1) * (a + b)
elif j == n - 1:
c += l * (a + b) + a
elif l == 1:
c += a + 2 * b
else:
c += min(2 * a + l * a + (l - 1) * b + 2 * b, l * a + l * 2 * b)
else:
while j + 1 < n and s[j + 1] == 1:
j += 1
l = j - i + 1
c += l * a + 2 * l * b
i = j + 1
z = not z
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR IF VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def next_one(s, current):
for i in range(current + 1, len(s)):
if int(s[i]) == 1:
return i
return -1
t = int(input())
for i in range(t):
n, a, b = map(int, input().split())
s = input()
changed_dig = False
price = n * a
for j in range(n - 1):
if not changed_dig:
current = s[j]
if int(current) == 0 and int(s[j + 1]) == 0:
price += b
current = -1
elif int(current) == 0 and int(s[j + 1]) == 1:
price += 2 * b + a
current = -1
elif int(current) == 1 and int(s[j + 1]) == 0:
if next_one(s, j) == -1:
price += 2 * b + a
current = -1
else:
diff = next_one(s, j) - j - 2
if diff * b < 2 * a:
price += 2 * b
current = 1
else:
price += 2 * b + a
current = -1
elif int(current) == 1 and int(s[j + 1]) == 1:
price += 2 * b
current = -1
changed_dig = False
if current == 1:
changed_dig = True
print(price + 2 * b) | FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
an = []
for i in range(t):
n, a, b = map(int, input().split())
s = input()
m = n * a + (n + 1) * b
y = []
l = 0
for i in range(n):
if s[i] == "0":
l += 1
elif l != 0:
y += [l]
l = 0
y += [l]
m += (len(y) - 1 + s.count("1")) * b
if len(y) != 1:
m += 2 * a
for j in range(1, len(y) - 1):
m += min((y[j] - 1) * b, 2 * a)
an.append(m)
for i in an:
print(i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR LIST VAR ASSIGN VAR NUMBER VAR LIST VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for _ in range(t):
n, a, b = map(int, input().split())
s = input()
INF = 10**18
dp = [([INF] * 2) for i in range(n + 1)]
dp[0][0] = b
for i in range(1, n + 1):
if i < n and (s[i] == "1" or s[i - 1] == "1"):
dp[i][0] = INF
dp[i][1] = min(dp[i - 1][0] + 2 * a, dp[i - 1][1] + a) + b * 2
else:
dp[i][0] = min(dp[i - 1][1] + 2 * a, dp[i - 1][0] + a) + b
dp[i][1] = min(dp[i - 1][1] + a, dp[i - 1][0] + 2 * a) + 2 * b
print(dp[n][0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for _ in range(t):
n, a, b = map(int, input().split())
s = input()
cnt = 0
first_one = 0
last_one = 0
first_zero = 0
last_zero = 0
diff = 0
for i in range(n):
if s[i] == "1":
if first_one == 0:
first_one = i
last_one = i
if a * 2 < b * (last_zero - first_zero):
diff += a * 2 - b * (last_zero - first_zero)
first_zero = 0
last_zero = 0
if s[i] == "0":
if first_one != 0:
if first_zero == 0:
first_zero = i
last_zero = i
if first_one == 0:
ans = a * n + b * (n + 1)
else:
ans = a * (n + 2) + b * (n + 1 + (last_one - first_one + 2)) + diff
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for z in range(t):
n, a, b = map(int, input().split())
arr = list(map(int, list(input())))
if not 1 in arr:
print((n + 1) * b + n * a)
continue
dst = 0
need = (2 * a + b - 1) // b
s = 0
first = arr.index(1)
s = first * (a + b) + a + 2 * b + a
for i in range(first + 1, n):
if arr[i] == 1:
if dst == 0:
s += 2 * b + a
else:
s += min(
(dst - 1) * (a + b) + 4 * (a + b),
(dst - 1) * (a + 2 * b) + 4 * b + 2 * a,
)
dst = 0
else:
dst += 1
s += dst * (a + b) + 2 * (a + b) - a
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
read_line = lambda: sys.stdin.readline().strip()
read_intmap = lambda: map(int, read_line().split())
def testCase():
n, a, b = read_intmap()
cross = tuple(map(int, read_line()))
cost = n * a + (n + 1) * b
dp = [([0] * (n + 1)) for i in range(2)]
upper = max(a, b) * n * 20
dp[0][-1] = 0
dp[1][-1] = upper
for i in range(n - 1, 0, -1):
if cross[i] or cross[i - 1]:
dp[0][i] = upper
else:
dp[0][i] = min(dp[1][i + 1] + a, dp[0][i + 1])
dp[1][i] = b + min(dp[0][i + 1] + a, dp[1][i + 1])
dp[0][0] = min(dp[1][1] + a, dp[0][1])
print(dp[0][0] + cost)
t = 1
t = int(input())
for i in range(t):
testCase() | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | from sys import stdin, stdout
t = int(stdin.readline().strip())
for _ in range(t):
n, a, b = stdin.readline().strip().split(" ")
n, a, b = int(n), int(a), int(b)
s = stdin.readline().strip()
h1 = a + b
h2 = 2 * a + b
for i in range(1, len(s)):
if s[i] == "0":
if h1 == None:
h1 = h2 + 2 * b + 2 * a
h2 = h2 + 2 * b + a
else:
t1 = min(h1 + a + b, h2 + 2 * a + 2 * b)
h2 = min(h2 + 2 * b + a, h1 + b + 2 * a)
h1 = t1
else:
h2 = h2 + 2 * b + a
h1 = None
ans = h1 + b
stdout.write(str(ans) + "\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NONE ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def find_next(A, i, number, n):
while i < n + 1 and A[i] != number:
i += 1
return i
for _ in range(int(input())):
n, a, b = [int(i) for i in input().split()]
A = [int(i) for i in input()]
A.append(0)
cost = b * (n + 1) + n * a
tccost = 2 * a
strtc = a
i = 0
i = find_next(A, i, 1, n)
if i == n + 1:
print(cost)
continue
cost += strtc
h2s = i
i = find_next(A, i, 0, n)
cost += (i - h2s + 1) * b
while i < n + 1:
prev = i
i = find_next(A, i, 1, n)
if i == n + 1:
break
if i - prev == 1:
pass
else:
cost += min(tccost, b * (i - prev - 1))
h2s = i
i = find_next(A, i, 0, n)
cost += (i - h2s + 1) * b
cost += strtc
print(cost) | FUNC_DEF WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | T = int(input())
for _ in range(T):
n, a, b = map(int, input().split())
s = input()
cost = b
road = 0
previousheight = 0
for c in s:
road += 1
if c == "1":
if road > 2 or road > 1 and previousheight == 0:
optionH = 0
optionL = 0
pilars = road
pipelinesH = road
pipelinesL = road
if previousheight == 0:
optionH += 2 * a
optionL += 2 * a
pipelinesH -= 1
pipelinesL -= 1
else:
optionL += 4 * a
pipelinesL -= 2
optionH += pipelinesH * a
optionL += pipelinesL * a
optionH += pilars * 2 * b
optionL += (pilars + 2) * b
minOp = min(optionH, optionL)
cost += minOp
else:
cost += road * a + road * 2 * b
previousheight = 1
road = 0
cost += a
if previousheight == 1:
cost += a
cost += (road - 1) * a
optionL = road * b
optionH = (road - 1) * 2 * b + b
minOp = min(optionH, optionL)
cost += minOp
print(cost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
n, a, b = (int(s) for s in input().split())
s = input().strip()
ans = (n + 1) * b + n * a
s1 = [i for i in s.split("0") if i != ""]
s2 = [i for i in s.split("1") if i != ""]
for i in range(len(s1)):
if i == 0:
ans += a
if i == len(s1) - 1:
ans += a
ans += (len(s1[i]) + 1) * b
for j in range(1, len(s2) - 1):
if len(s2[j]) > 1:
ans += min((len(s2[j]) - 1) * b, 2 * a)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR STRING VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for _ in range(t):
n, a, b = map(int, input().split())
s = str(input())
last_seen = "0"
count = 0
counts = []
for i in range(len(s)):
if s[i] == last_seen:
count += 1
else:
counts.append(count)
last_seen = s[i]
count = 1
counts.append(count)
if len(counts) == 1:
print((n + 1) * b + n * a)
continue
total = 0
total += counts[0] * b + (counts[0] + 1) * a
total += counts[-1] * b + (counts[-1] + 1) * a
counts = counts[1:-1]
ind = 0
while True:
total += 2 * (counts[ind] + 1) * b + counts[ind] * a
if ind == len(counts) - 1:
break
cost1 = (counts[ind + 1] - 1) * b + (counts[ind + 1] + 2) * a
cost2 = 2 * (counts[ind + 1] - 1) * b + counts[ind + 1] * a
total += min(cost1, cost2)
ind += 2
print(total) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for _ in range(t):
n, a, b = map(int, input().split())
s = input()
M = min(a, b)
i = s.find("1")
if i != -1:
Cost = i * (a + b) + a + 2 * b
else:
Cost = n * a + (n + 1) * b
print(Cost)
continue
prev = i
while i < n:
while i < n and s[i] == "1":
prev = i
i += 1
Cost += 2 * b + a
while i < n and s[i] != "1":
i += 1
if i == n:
diff = i - prev - 1
Cost += diff * a
Cost += (diff - 1) * b
Cost += a + b
else:
diff = i - prev - 1
Cost += diff * a
Cost += (diff - 1) * b
Cost += min(2 * a, (diff - 1) * b) + 2 * b
print(Cost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR WHILE VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for _ in range(t):
n, a, b = [int(x) for x in input().split()]
s = input()
ones = []
for i in range(len(s)):
if s[i] == "1":
ones.append(i)
if len(ones) == 0:
cost = n * a + (n + 1) * b
print(cost)
continue
cost = (ones[0] + 1) * a + (ones[0] + 2) * b
for i in range(1, len(ones)):
if ones[i - 1] + 1 == ones[i]:
cost += a + b + b
elif ones[i - 1] + 2 == ones[i]:
cost += 2 * (a + b + b)
else:
z = ones[i] - ones[i - 1] - 1
x = (z + 1) * (a + b + b)
y = a + b + b + (2 + z) * a + (z + 1) * b
cost += min(x, y)
z = n - ones[-1] - 1
cost += (z + 2) * (a + b)
print(cost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
def naiveSolve():
return
def main():
t = int(input())
allans = []
for _ in range(t):
n, a, b = readIntArr()
s = input()
baseCost = n * a + (n + 1) * b
n += 1
s2 = [0] * n
for i in range(n - 1):
if s[i] == "1":
s2[i] = s2[i + 1] = 1
dp = [[inf, inf] for _ in range(n)]
dp[0][0] = 0
for i in range(1, n):
dp[i][1] = min(dp[i - 1][0] + a + b, dp[i - 1][1] + b)
if s2[i] == 0:
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1] + a)
ans = dp[n - 1][0] + baseCost
allans.append(ans)
multiLineArrayPrint(allans)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultValFactory, dimensionArr):
dv = defaultValFactory
da = dimensionArr
if len(da) == 1:
return [dv() for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(i, j):
print("? {} {}".format(i, j))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(" ".join([str(x) for x in ans])))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main() | IMPORT FUNC_DEF RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | ans = []
round = int(input())
while round > 0:
register = {"high": 0, "half": 0, "low": 0}
[road_len, pp_cost, plr_cost] = list(map(int, input().split()))
road = [int(i) for i in input()]
trade_off = 1
while 2 * pp_cost > trade_off * plr_cost:
trade_off += 1
trade_off += 1
counter = 0
pre = -1
for i in range(len(road)):
cur = road[i]
if cur == 1 and pre == 0:
if register["high"] == 0:
register["low"] += counter - 1
register["high"] += 1
register["half"] += 1
counter = 0
else:
register["high"] += 1
if counter >= trade_off:
register["low"] += counter - 2
register["half"] += 2
else:
register["high"] += counter
counter = 0
elif i == len(road) - 1:
if register["high"] == 0:
register["low"] += counter + 1
else:
register["half"] += 1
register["low"] += counter
elif cur == 0:
counter += 1
elif cur == 1:
register["high"] += 1
counter = 0
pre = cur
pipelines = register["high"] + 2 * register["half"] + register["low"]
pillars = (
4 * register["high"] + 3 * register["half"] + 2 * register["low"] - 2
) / 2 + 2
cost = pipelines * pp_cost + pillars * plr_cost
ans.append(cost)
round -= 1
for i in ans:
print(int(i)) | ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN LIST VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR STRING NUMBER VAR STRING BIN_OP VAR NUMBER VAR STRING NUMBER VAR STRING NUMBER ASSIGN VAR NUMBER VAR STRING NUMBER IF VAR VAR VAR STRING BIN_OP VAR NUMBER VAR STRING NUMBER VAR STRING VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR STRING NUMBER VAR STRING BIN_OP VAR NUMBER VAR STRING NUMBER VAR STRING VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING BIN_OP NUMBER VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR STRING BIN_OP NUMBER VAR STRING BIN_OP NUMBER VAR STRING NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | from sys import stdin
input = stdin.readline
a = []
ans = []
for _ in range(int(input())):
n, p, q = map(int, input().split())
s = input()
l = list(s)
cnt = 0
if s.count("1") == 0:
print(q * (n + 1) + p * n)
continue
a = [0]
b = [1]
for i in range(1, n):
if s[i] == s[i - 1]:
b[-1] += 1
continue
else:
a.append(int(s[i]))
b.append(1)
cp = 1
ans = q
for i in range(len(a)):
n = b[i]
if i == len(a) - 1:
ans += (n + 1) * p + q * n
break
if a[i] == 0:
if cp == 1:
ans += p * (n + 1)
ans += (n - 1) * q
cp = 2
else:
ans += min((n + 2) * p + (n - 1) * q, n * p + (n - 1) * (2 * q))
cp = 2
else:
ans += n * p + 2 * q * (n + 1)
print(ans) | ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | n = int(input())
for i in range(n):
q = 0
w = 0
su = 0
d, c, st = [int(i) for i in input().split()]
s = input()
k = 0
flag = 1
for i in range(len(s)):
if s[i] == "0":
k += 1
elif flag == 1:
su += 2 * st + c
flag = 2
k = 0
else:
if k < 2:
su += (k + 1) * st
else:
su += min((k - 1) * st, 2 * c) + 2 * st
k = 0
if flag == 2:
su += c
su += st * (d + 1) + d * c
print(su) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def next_int():
return int(input())
def next_int_arr():
return map(int, input().split())
INF = 1 << 60
for _ in range(next_int()):
n, a, b = next_int_arr()
s = input()
dp0, dp1 = b, INF
for ch in s:
if ch == "1":
dp0, dp1 = INF, dp1 + 2 * b
else:
dp0, dp1 = min(dp0, dp1 + a) + b, min(dp1, dp0 + a) + 2 * b
print(dp0 + a * n) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | from sys import stdin
def ler():
for line in stdin:
yield line.rstrip()
def converte(string):
res = []
anterior = "0"
q = 0
for s in string:
if anterior == s:
q += 1
else:
res.append(q)
q = 1
anterior = s
res.append(q)
return res
def solve(s):
n, cano, pilar = [int(x) for x in next(s).split(" ")]
custo_subida = 2 * cano + 2 * pilar
custo_descida = 2 * cano + pilar
custo_alto = cano + 2 * pilar
custo_baixo = cano + pilar
string = next(s)
contagem = converte(string)
if len(contagem) == 1:
return custo_baixo * contagem[0] + pilar
custo = pilar + custo_subida + custo_baixo * (contagem.pop(0) - 1)
custo += custo_descida + custo_baixo * (contagem.pop() - 1)
while contagem:
alto = contagem.pop()
custo += custo_alto * alto
if not contagem:
break
baixo = contagem.pop()
if baixo == 1:
custo += custo_alto
continue
se_descer = custo_descida + custo_subida + custo_baixo * (baixo - 2)
se_ficar = custo_alto * baixo
custo += min(se_ficar, se_descer)
return custo
s = ler()
T = int(next(s))
for i in range(T):
print(solve(s)) | FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for i in range(int(input())):
n, a, b = map(int, input().split())
s = input()
i = 1
cost = a + b + b
prev = 0
while i < n and s[i] == "0":
cost += a + b
i += 1
while i < n - 1:
if s[i] == "1":
j = i
c = 1
while j < n and s[j] == "1":
c += 1
j += 1
cost += 2 * c * b + c * a
if prev == 0:
cost += a
i = j
prev = 1
else:
j = i
c = -1
while j < n and s[j] == "0":
c += 1
j += 1
if c == 0:
i = j
continue
i = j
l = c * b * 2 + c * a
m = c * b + 2 * a + c * a
if j == n:
cost += c * b + c * a + a
prev = 0
continue
if l <= m:
prev = 1
cost += l
else:
cost += m - a
prev = 0
if prev == 1:
cost += a
print(cost) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
n, a, b = map(int, input().split())
s = input()
dp = [([0] * 2) for _ in range(n + 1)]
for i in range(n):
if s[i] == "1":
dp[i + 1][0] = 10**18
else:
dp[i + 1][0] = min(dp[i][0] + a + b, dp[i][1] + 2 * a + 2 * b)
if i == 0:
dp[i + 1][1] = dp[i][0] + 2 * a + b
elif s[i] == "1":
dp[i + 1][1] = dp[i][1] + a + 2 * b
else:
dp[i + 1][1] = min(dp[i][0] + 2 * a + b, dp[i][1] + a + 2 * b)
print(dp[n][0] + b) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
n, a, b = map(int, input().split())
s = input()
d = list(s)
for i in range(1, n - 1):
if d[i] == "0" and d[i - 1] == "1" and d[i + 1] == "1":
d[i] = "1"
i = 0
while i < n and d[i] == "0":
i += 1
k = n - 1
while k >= 0 and d[k] == "0":
k -= 1
j = i
while j <= k:
c = 0
ss = j
while j <= k and d[j] == "0":
c += 1
j += 1
if c > 1 and (c - 1) * b < 2 * a:
while ss < j:
d[ss] = "1"
ss += 1
j += 1
onc = 0
ii = 0
while ii < n:
cc = 0
while ii < n and d[ii] == "1":
cc += 1
ii += 1
if cc > 0:
onc += cc + 1
ii += 1
onewale = n + 1 - onc
pillar = (onewale + onc * 2) * b
cc = 0
for l in range(1, n):
if d[l] != d[l - 1]:
cc += 1
pipe = (n + cc) * a
print(pillar + pipe) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | from sys import stdin
T = int(stdin.readline())
for i in range(0, T):
n, a, b = (int(j) for j in stdin.readline().split())
s = stdin.readline()
cost = (n + 1) * b + n * a
zero = list()
one = list()
zero_count = 0
one_count = 0
for j in s:
if j == "0":
zero_count += 1
if one_count != 0:
one.append(one_count)
one_count = 0
if j == "1":
one_count += 1
if zero_count != 0:
zero.append(zero_count)
zero_count = 0
if len(zero) == 0:
print(cost)
else:
cost += 2 * a
for j in one:
cost += (j + 1) * b
for j in range(1, len(zero)):
cost += min(2 * a, (zero[j] - 1) * b)
print(cost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR FOR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def solve():
l = list(map(int, input().strip().split()))
s = input()
n = l[0]
a = l[1]
b = l[2]
dp = []
for _ in range(n + 10):
dp.append([float("inf"), float("inf")])
dp[0][0] = b
dp[0][1] = float("inf")
for i in range(n):
if s[i] == "0":
dp[i + 1][0] = min(dp[i][0] + a + b, dp[i][1] + 2 * a + b)
dp[i + 1][1] = min(dp[i][0] + 2 * a + 2 * b, dp[i][1] + a + 2 * b)
else:
dp[i + 1][0] = float("inf")
dp[i + 1][1] = dp[i][1] + a + 2 * b
print(dp[n][0])
t = int(input())
for _ in range(t):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
while t:
n, a, b = [int(x) for x in input().split()]
road = input()
what = True
j = 0
answer = 0
first = 0
while j < n and road[j] != "1":
j += 1
first = j
for i in range(j + 1, n):
if road[i] == "0" and what:
x = i - j
answer += (x + 1) * 2 * b + x * a
j = i
what = False
elif not what and road[i] == "1":
x = i - j
answer += min(a * x + (x - 1) * b * 2, (x + 2) * a + (x - 1) * b)
j = i
what = True
begin = 0
for i in road:
if i == "1":
break
begin += 1
alpha = a + begin * a + begin * b
end = 0
for i in reversed(road):
if i == "1":
break
end += 1
if road.count("1") != 0:
beta = a + end * a + end * b
else:
alpha -= a
beta = b
print(answer + alpha + beta)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
n, a, b = map(int, input().split())
s = input()
first_1 = s.find("1")
last_1 = s.rfind("1")
cost = n * a + (n + 1) * b
if first_1 > -1:
cost += 2 * a
cost += (last_1 - first_1 + 2) * b
groups = []
cur = ""
for c in s[first_1 : last_1 + 1]:
if c == "0":
cur += c
else:
groups.append(cur)
cur = ""
for group in groups:
if len(group) < 2:
continue
new_cost = 2 * a - (len(group) - 1) * b
if new_cost < 0:
cost += new_cost
print(cost) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for _ in range(t):
n, a, b = map(int, input().split())
l = input()
x = 0
y = a + b
for i in range(1, n):
if l[i] == "1":
x = 10000000000000000
c = x
if l[i] == "0":
x = min(x + a + b, y + 2 * a + b)
y = min(c + 2 * a + 2 * b, y + a + 2 * b)
print(x + a + 2 * b) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for t in range(int(input())):
n, a, b = [int(j) for j in input().split()]
ans = 0
prev = -1
last_state = 0
l = [int(j) for j in list(input())]
prev = 0
p = [(0) for i in range(n)]
flag = False
for i in range(n)[::-1]:
if l[i] == 1 and flag:
p[i] = prev
prev = i
if not flag and l[i] == 1:
flag = True
prev = i
p[i] = 0
if l[i] == 0:
p[i] = prev
curr_state = 0
ans = b
jump = False
for i in range(n - 1):
if jump:
jump = False
ans += 2 * a + b
elif l[i + 1] == 0:
if curr_state == 0:
ans += a + b
else:
ans += a + 2 * b
if 2 * a <= (p[i] - i - 2) * b or p[i] == 0:
jump = True
curr_state = 0
elif l[i + 1] == 1:
if curr_state == 1:
ans += a + 2 * b
else:
ans += 2 * a + 2 * b
curr_state = 1
if curr_state == 1 or jump:
ans += 2 * a + b
else:
ans += a + b
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | q = int(input())
for _ in range(q):
n, a, b = map(int, input().split())
s = input()
f = 0
cost = b
cost2 = 0
for i in range(n):
if s[i] == "0":
if f:
cost2 += a + 2 * b
cost += a + b
if i + 1 < n:
if s[i + 1] == "1":
cost += a + b
else:
if cost2:
cost = min(cost, cost2)
cost2 = 0
cost += a + 2 * b
if i + 1 < n:
if s[i + 1] == "0":
cost2 = cost
if i == 0:
cost2 += b
cost += a
f = 1
if cost2:
if i == n - 1:
cost2 = cost2 + a - b
cost = min(cost, cost2)
print(cost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | T = int(input())
for test_num in range(T):
n, a, b = map(int, input().split())
st = 0
ln = input()
per = 0
cx = 0
cn = 0
i = 0
while i < len(ln):
if ln[i] == "1":
if cn > 0:
if per > 0:
st = st + min(
cn * a + (cn + 1) * 2 * b, (cn + 2) * a + (cn + 3) * b
)
else:
st = st + (i + 1) * a + (i + 2) * b
cx = 1
cn = 0
else:
cx = cx + 1
per = per + 1
elif cn > 0:
cn = cn + 1
else:
if per > 0:
st = st + cx * a + (cx - 1) * 2 * b
cn = 1
cx = 0
i = i + 1
if per > 0:
st = st + (cn + 1) * a + (cn + 2) * b
else:
st = st + i * a + (i + 1) * b
print(st) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for _ in range(t):
n, a, b = map(int, input().rstrip().split())
s = input() + "0"
m1, m2 = b, float("inf")
for i in range(n):
m3 = min(m1 + 2 * a + 2 * b, m2 + a + b * 2)
if s[i + 1] == "0" and s[i] == "0":
m1 = min(m1 + a + b, m2 + 2 * a + b)
else:
m1 = float("inf")
m2 = m3
print(m1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | T = int(input())
for k in range(T):
n, a, b = map(int, input().split())
s = input()
dp = [[0, 0] for k in range(n + 1)]
dp[0][0] = b
dp[0][1] = float("inf")
for l in range(1, n + 1):
if s[l - 1] == "0":
dp[l][0] = min(dp[l - 1][0] + a + b, dp[l - 1][1] + 2 * a + 2 * b)
dp[l][1] = min(dp[l - 1][1] + a + 2 * b, dp[l - 1][0] + 2 * a + 2 * b)
else:
dp[l][0] = float("inf")
dp[l][1] = min(dp[l - 1][1] + a + 2 * b, dp[l - 1][0] + 2 * a + 2 * b)
print(dp[n][0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def handle_case(n, pipe, pillar, s):
num_pipe = 0.5
num_pillar = 1
last = "down"
def increase_by(num_pipe, pipe_inc, num_pillar, pillar_inc):
num_pipe += pipe_inc
num_pillar += pillar_inc
return num_pipe, num_pillar
def is_good_to_down(i):
nearest_one_indx = s[i:].find("1")
if nearest_one_indx == -1:
return "down", i, 2, 2
nearest_minus_2 = nearest_one_indx - 1
cost_of_down = 4 * pipe + 4 * pillar + nearest_minus_2 * (pipe + pillar)
cost_of_up = 2 * pipe + 4 * pillar + nearest_minus_2 * (pipe + 2 * pillar)
if cost_of_down < cost_of_up:
return (
"up",
nearest_one_indx + i,
4 + nearest_minus_2,
4 + nearest_minus_2,
)
else:
return (
"up",
nearest_one_indx + i,
2 + nearest_minus_2,
4 + nearest_minus_2 * 2,
)
i = 1
while i < len(s):
if "".join([s[i - 1 : i + 1]]) == "11":
num_pipe, num_pillar = increase_by(num_pipe, 1, num_pillar, 2)
elif "".join([s[i - 1 : i + 1]]) == "01":
if last == "down":
num_pipe, num_pillar = increase_by(num_pipe, 2, num_pillar, 2)
last = "up"
else:
num_pipe, num_pillar = increase_by(num_pipe, 1, num_pillar, 2)
elif "".join([s[i - 1 : i + 1]]) == "00":
if last == "down":
num_pipe, num_pillar = increase_by(num_pipe, 1, num_pillar, 1)
else:
num_pipe, num_pillar = increase_by(num_pipe, 1, num_pillar, 2)
elif "".join([s[i - 1 : i + 1]]) == "10":
last, i, pipe_inc, pillar_inc = is_good_to_down(i)
num_pipe, num_pillar = increase_by(
num_pipe, pipe_inc, num_pillar, pillar_inc
)
i += 1
num_pipe += 0.5
num_pillar += 1
cost = pipe * num_pipe + pillar * num_pillar
print(int(cost))
def run():
T = eval(input())
queries = []
for i in range(T):
n, a, b = list(map(int, input().split()))
s = input()
queries.append((n, a, b, s))
for query in queries:
n, a, b, s = query
handle_case(n, a, b, s)
run() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FUNC_DEF VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER RETURN STRING VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR RETURN STRING BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR RETURN STRING BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL STRING LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL STRING LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL STRING LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL STRING LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for nt in range(t):
n, a, b = map(int, input().split())
s = input()
group = []
current = "0"
count = 0
for i in range(n):
if s[i] == current:
count += 1
else:
group.append(count)
count = 1
if current == "0":
current = "1"
else:
current = "0"
if current == "0":
group.append(count)
cost = 0
if len(group) != 1:
cost += (group[0] + 1) * a + group[0] * b
for i in range(1, len(group) - 1):
if i % 2 == 1:
cost += group[i] * a + group[i] * b * 2
else:
cost += min(
(group[i] + 2) * a + (group[i] + 1) * b,
group[i] * a + group[i] * b * 2,
)
cost += (group[-1] + 2) * b + (group[-1] + 1) * a
print(cost)
else:
print(n * a + (n + 1) * b) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | import sys
input = sys.stdin.readline
def getInt():
return int(input())
def getVars():
return map(int, input().split())
def getArr():
return list(map(int, input().split()))
def getStr():
return input().strip()
t = getInt()
for _ in range(t):
n, a, b = getVars()
s = getStr()
p = s.find("1")
res = n * a + (n + 1) * b
if p > -1:
res += 2 * a
while p > 0:
p0 = s.find("0", p)
res += (p0 - p + 1) * b
p = s.find("1", p0)
if p > -1:
res += min((p - p0 - 1) * b, 2 * a)
print(res) | IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | x = int(input())
for i in range(x):
n, a, b = map(int, input().split())
queue = input()
first = queue.find("1")
if first == -1:
print(a * n + b * (n + 1))
continue
backqueue = queue[::-1]
last = n - 1 - backqueue.find("1")
save = (2 * a - 1) // b + 2
conseczeroes = 0
total = (n - last + first + 1) * (a + b)
while first <= last:
if queue[first] == "1":
if conseczeroes >= save:
total += (conseczeroes + 3) * (a + b)
else:
total += (conseczeroes + 1) * (a + 2 * b)
conseczeroes = 0
else:
conseczeroes += 1
first += 1
print(str(total)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR WHILE VAR VAR IF VAR VAR STRING IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
N, pipe, pillar = map(int, input().split())
S = input()
Bins = []
flag = True
for i in range(len(S)):
if S[i] == "0" and flag:
flag = False
O = i
elif S[i] == "1" and not flag:
flag = True
Bins.append((O, i - 1))
if S[-1] == "0":
Bins.append((O, len(S) - 1))
Cost = 2 * N * pillar + (N + 2) * pipe
if S.count("0") == N:
print((N + 1) * pillar + N * pipe)
continue
if Bins[0][1] == 0:
pass
else:
Cost -= Bins[0][1] * pillar
for i in range(1, len(Bins) - 1):
X = Bins[i][1] - Bins[i][0]
if X == 0:
continue
elif 2 * pipe - X * pillar < 0:
Cost += 2 * pipe - X * pillar
if Bins[-1][0] == Bins[-1][1]:
pass
else:
Cost -= (Bins[-1][1] - Bins[-1][0]) * pillar
print(Cost) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR STRING VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
while t != 0:
t -= 1
n, a, b = map(int, input().split())
s = input()
dp = [[(10**20) for _ in range(n + 1)] for _ in range(2)]
dp[0][0] = b
for i in range(n):
if s[i] == "0":
dp[0][i + 1] = min(dp[0][i] + a + b, dp[1][i] + 2 * a + b)
dp[1][i + 1] = min(dp[1][i] + a + 2 * b, dp[0][i] + 2 * (a + b))
else:
dp[1][i + 1] = dp[1][i] + a + 2 * b
print(dp[0][n]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for query in range(t):
n, a, b = map(int, input().split())
s = input()
A = list()
ln = 1
j = 1
while j < n:
if s[j] == s[j - 1]:
ln += 1
else:
A.append(ln)
ln = 1
j += 1
A.append(ln)
if len(A) == 1:
print(a * n + b * n + b)
else:
ans = A[0] * b + n * a
i = 1
while i + 1 < len(A):
if i % 2 == 1:
ans += (A[i] + 1) * 2 * b + a
else:
ans += (A[i] - 1) * b + min(a, (A[i] - 1) * b - a)
i += 1
ans += A[-1] * b + a
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | def int_multiple():
return [int(c) for c in input().split()]
def int_single():
return int(input())
def str_multiple():
return [c for c in input().split()]
def str_single():
return input()
T = int_single()
res = []
for i in range(T):
n, pipe, pillar = int_multiple()
s = str_single()
r = 0
fs = -1
ls = -1
for j in range(n):
if s[j] == "1":
fs = j
break
for j in reversed(range(n)):
if s[j] == "1":
ls = j
break
if ls < 0:
r = (n + 1) * pillar + n * pipe
elif ls - fs <= 1:
r = (n + 1) * pillar + n * pipe + 2 * pipe + (ls - fs + 2) * pillar
else:
r += (fs - 1) * pipe + 2 * pipe + fs * pillar
r += (n - 2 - ls) * pipe + 2 * pipe + (n - 1 - ls) * pillar
ones = 0
zeros = 0
for j in range(fs, ls + 1):
if s[j] == "1":
if zeros >= 2:
opt1 = (zeros - 2) * pipe + 4 * pipe + (zeros - 1) * pillar
opt2 = zeros * pipe + (zeros - 1) * 2 * pillar
choosen = min(opt1, opt2) + (ones + 1) * 2 * pillar + ones * pipe
r += choosen
ones = 1
zeros = 0
else:
ones += zeros + 1
zeros = 0
else:
zeros += 1
last_sum = ones * pipe + (ones + 1) * pillar * 2
r += last_sum
res.append(r)
for w in res:
print(w) | 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 VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | t = int(input())
for _ in range(t):
n, a, b = list(map(int, input().split()))
s = input()
a1 = 0
start = 1
total = 0
if "1" in s:
for i in range(1, n - 1):
if s[i] == "0":
start += 1
else:
break
total += (start + 1) * a + start * b + 2 * b
end = n - 2
for i in range(n - 2, start, -1):
if s[i] == "0":
end = end - 1
else:
break
total += (n - 1 - end + 1) * a + (n - 1 - end) * b
j = start
c = 0
while j <= end:
if s[j] == "1":
total += a + 2 * b
j += 1
else:
while s[j] == "0":
j += 1
c += 1
if c == 1:
total += a + 2 * b
else:
total += (
min(a * (c + 2) + (c - 1) * b, a * c + (c - 1) * 2 * b) + 2 * b
)
c = 0
print(total)
else:
print(n * a + (n + 1) * b) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units.
[Image]
Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
-----Input-----
The fist line contains one integer $T$ ($1 \le T \le 100$) β the number of queries. Next $2 \cdot T$ lines contain independent queries β one query per two lines.
The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β the description of the road.
It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers β one per query. For each query print the minimum possible cost of the constructed pipeline.
-----Example-----
Input
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output
94
25
2900000000
13
-----Note-----
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
[Image]
The optimal (and the only possible) pipeline for the third query is shown below:
[Image]
The optimal pipeline for the fourth query is shown below:
[Image] | for _ in range(int(input())):
n, a, b = [int(i) for i in input().split()]
data = input() + "0"
inf = 1 << 50
dp = [[0, inf]]
for i in range(1, n + 1):
dp.append([0, 0])
if data[i] == "0" and data[i - 1] == "0":
dp[-1][0] = min(dp[-2][0], dp[-2][1] + a)
dp[-1][1] = min(dp[-2][1] + b, dp[-2][0] + a + b)
else:
dp[-1][0] = inf
dp[-1][1] = min(dp[-2][1] + b, dp[-2][0] + a + b)
print(dp[-1][0] + b + n * (a + b)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.