description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = [int(j) for j in input().split()] a = input().split("1") p = 0 for i in a: if "0" in i: p += 1 if p == 0: print(0) else: print((p - 1) * min(x, y) + y)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF STRING VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() cnt = 0 i = 0 while i < n: if s[i] == "0": cnt += 1 while i < n and s[i] == "0": i += 1 i += 1 ans = 10**18 for i in range(cnt, 0, -1): val = i * y + (cnt - i) * x ans = min(ans, val) if ans == 10**18: print(0) else: print(ans)
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 IF VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) a = str(input()) f = 1 d = 0 if a.find("0") == -1: print(0) else: for i in a: if i == "0" and f == 1: d += 1 f = 0 elif i == "1": f = 1 print((d - 1) * min(x, y) + y)
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 IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def r(a, b): ans = a // b if a % b != 0: ans += 1 return ans n, x, y = map(int, input().split()) a = list(input()) + ["1"] count = 0 for i in range(1, n + 1): if a[i] == "1" and a[i - 1] == "0": count += 1 ans = 999999999999999999 if count == 0: print(0) exit() for i in range(count): ans = min(ans, min(x, y) * i + y * (count - i)) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST STRING 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 ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = [int(s) for s in input().split(" ")] a = input() z = len([t for t in a.split("1") if t]) ans = max(min(z * y, y + (z - 1) * x), 0) print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
[n, x, y] = map(int, input().strip().split()) a = input().strip() + "1" k = a.count("01") if k == 0: print(0) else: print((k - 1) * min(x, y) + y)
ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split(" ")) s = input() isolated = 0 ones = 0 last = "1" for digit in s: if digit == "1" and last == "0": isolated += 1 if digit == "0": ones += 1 last = digit if last == "0": isolated += 1 if ones == 0: print(0) else: print(min(isolated * y, (isolated - 1) * x + y))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
from itertools import groupby n, x, y = map(int, input().split()) s = input().strip() b = [(1 if k == "1" else 0) for k, g in groupby(s)] dp = [0] * len(b) dp[0] = y if b[0] == 0 else 0 for i in range(1, len(b)): if b[i] == 0: dp[i] = dp[i - 1] + y if i >= 2: dp[i] = min(dp[i], x + dp[i - 2]) else: dp[i] = dp[i - 1] print(dp[-1])
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() count = 0 if s[0] == "1": tp = -1 else: tp = 0 count = 1 for i in range(1, n): if s[i] == "0": if tp == -1: tp = 0 count += 1 elif tp == 0: tp = -1 if x >= y: print(count * y) elif count >= 2: print((count - 1) * x + y) elif count == 1: print(y) else: print(0)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) a = ["1"] + list(input()) c = 0 for i in range(n): if a[i : i + 2] == ["1", "0"]: c += 1 print(max(c - 1, 0) * min(x, y) + (y if c >= 1 else 0))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER LIST STRING STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() c = len([_ for _ in s.split("1") if _]) ans = 1e19 if not c: ans = 0 for i in range(c): ans = min(ans, i * x + (c - i) * y) print(ans)
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 FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = list(map(int, input().split())) a = input() a = a + "1" cnt = 0 for i in range(n): if a[i] == "0" and a[i + 1] == "1": cnt += 1 if cnt == 0: print(0) exit() print(min(y * cnt, y + (cnt - 1) * x))
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 VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) A = input() cnt = 0 k = A.find("0") while k < n and k != -1: while A[k] != "1": k += 1 if k >= n: break cnt += 1 k = A.find("0", k) if cnt == 0: print(0) else: print((cnt - 1) * min(x, y) + y)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) u = list(map(int, input())) d0 = 0 k0 = 0 for i in range(n): if k0 == 1 and u[i] == 1: d0 += 1 k0 = 0 elif u[i] == 0: k0 = 1 if k0 == 1: d0 += 1 if d0 == 0: p = 0 else: p = (d0 - 1) * min(x, y) + y print(p)
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() k = 0 if s[0] == "0": for i in range(n - 1): if s[i] + s[i + 1] == "01": k += 1 if s[-1] == "0": k += 1 else: for i in range(n - 1): if s[i] + s[i + 1] == "10": k += 1 if k == 0: print(0) else: print(min(k * y, (k - 1) * x + y))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) a = input() a = a.strip("1") n = len(a) c = 0 i = 0 while i < n: try: i1 = a.index("1", i) if x < y: try: i = a.index("0", i1) except: i = n c += x else: i = a.index("0", i1) c += y except: i = n c += y print(c)
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 VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) c = input() f = 0 t = 0 for i in range(n): if c[i] == "0": if not f: t += 1 f = 1 else: f = 0 if not t: print(0) else: print(y + (t - 1) * min(x, y))
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 IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = input().split() n = int(n) x = int(x) y = int(y) s = input() count = 0 is_zero = s[0] == "0" if is_zero: count += 1 for i in range(1, n): if s[i] == "0" and not is_zero: is_zero = True count += 1 elif s[i] == "1" and is_zero: is_zero = False if y <= x or count == 0: print(y * count) else: print(x * (count - 1) + y)
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER STRING IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) seq = input() segments = 0 flag = False for i in seq: if i == "0": if flag: continue else: flag = True elif flag: segments += 1 flag = False else: continue if flag: segments += 1 flag = False cost = segments * y for i in range(segments): currcost = i * x + (segments - i) * y if currcost < cost: cost = currcost print(cost)
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 VAR IF VAR STRING IF VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def readline(): return map(int, input().split()) def main(): n, x, y = readline() line = input() one = line.count("10") zero = line.count("01") assert abs(one - zero) < 2 if one != zero: segments_of_zeros = max(one, zero) else: segments_of_zeros = one + (line[0] == "0") print((segments_of_zeros - 1) * min(x, y) + y if segments_of_zeros else 0) main()
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
import time n, x, y = (int(i) for i in input().split()) a = input() start = time.time() I = 0 while a[I] == "1": I += 1 if I == n: break flag = 1 N = 0 for i in range(I, n): if flag == 0: if a[i] == "1": flag = 1 elif a[i] == "0": flag = 0 N += 1 if N == 0: print(0) elif x > y: print(N * y) else: print(y + x * (N - 1)) finish = time.time()
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def f(reverse_cost, inverse_cost, bit_string): prev_value = True zero_section_count = 0 for v in bit_string: cur_value = bool(v == "1") if prev_value != cur_value: if not cur_value: zero_section_count += 1 prev_value = cur_value if zero_section_count: return min(reverse_cost, inverse_cost) * (zero_section_count - 1) + inverse_cost else: return 0 n, x, y = [int(i) for i in input().split()] s = input() print(f(x, y, s))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = input().strip().split() n, x, y = int(n), int(x), int(y) s = input().strip() if n == 1 and s == "0": print(y) elif n == 1 and s == "1": print(0) else: l = [s[0]] for i in s[1:]: if i != l[-1]: l.append(i) t = l.count("0") if t == 0: print(0) else: print(min((t - 1) * x + y, t * y))
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
l = [int(x) for x in input().split()] n = input() x = 0 y = 0 if len(set(n)) == 1: if n[0] == "1": print(0) else: print(l[2]) else: for i in range(0, len(n)): if i == 0: if n[0] == "0": x += 1 y += 1 continue if n[i] == "1": continue if n[i] == "0" and n[i - 1] == "0": continue if n[i] == "0" and n[i - 1] == "1": x += 1 y += 1 costy = y * l[2] costx = (x - 1) * l[1] + l[2] print(min(costy, costx))
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = list(map(int, input().split(" "))) s = list(map(int, list(input()))) t = [] last0 = False isfirst = True for i in s: if i == 0: if isfirst: t.append(i) isfirst = False last0 = True elif last0: continue else: last0 = True t.append(i) else: if isfirst: t.append(i) isfirst = False last0 = False if not last0: continue else: last0 = False t.append(i) if x < y: s = 0 for i in t: if i == 0: s += 1 if s != 0: print((s - 1) * x + y) else: print(0) else: s = 0 for i in t: if i == 0: s += 1 print(s * y)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def numberofgroupsofzeroes(s): count = 0 if s[len(s) - 1] == "0": count = 1 for i in range(0, len(s) - 1): if s[i] == "0" and s[i + 1] == "1": count += 1 return count def minimum(num1, num2): if num1 <= num2: return num1 else: return num2 n, x, y = [int(e) for e in input().split()] a = input() groupnum = numberofgroupsofzeroes(a) if groupnum == 0: print(0) else: answer = (groupnum - 1) * minimum(x, y) + y print(answer)
FUNC_DEF ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = (int(x) for x in input().split()) s = input() k = s.count("01") if s.endswith("0"): k += 1 if k == 0: print(0) elif y < x: print(y * k) else: print(x * (k - 1) + y)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() c = 0 for i in range(1, n): if s[i] == "1" and s[i - 1] == "0": c += 1 if s[-1] == "0": c += 1 if c == 0: print(0) elif x >= y: print(c * y) else: print((c - 1) * x + y)
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 NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() chains = 0 zero = False for i in range(n): if not zero and s[i] == "0": zero = True elif not zero and s[i] == "1": continue elif zero and s[i] == "0": continue elif zero and s[i] == "1": chains += 1 zero = False if zero: chains += 1 if chains == 0: print(0) else: min_cost = 99999999999999999999999999 for n_invert in range(1, chains + 1): n_reverse = chains - n_invert cost = n_reverse * x + n_invert * y min_cost = min(min_cost, cost) print(min_cost)
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 VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR STRING IF VAR VAR VAR STRING IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
from itertools import groupby noc, x, y = [int(x) for x in input().split()] s = input() sg = groupby(s) sg = [(k, list(g)) for k, g in sg if set(list(g)) == {"0"}] lsg = len(sg) ans = (len(sg) - 1) * min(x, y) + y print(0 if lsg == 0 else ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) io = "1" c = 0 for i in input(): if i == "0": c += int(io != i) io = i if c > 0: print(min(y * c, y + x * (c - 1))) else: print(0)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR STRING VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = list(map(int, input().split())) a = input() k = 0 for i in range(n): if i == 0 or a[i] != a[i - 1]: if a[i] == "0": k += 1 if k == 0: print(0) else: print(min([(x * r + (k - r) * y) for r in range(k)]))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
header = input() header = [int(i) for i in header.split()] n = header[0] revCost = header[1] opCost = header[2] numsData = input() clusters = 0 i = 0 complete = False while numsData[i] == "1": i += 1 if i >= len(numsData): complete = True break if not complete: while True: while numsData[i] == "0": i += 1 if i >= len(numsData): break if i >= len(numsData): break while numsData[i] == "1": i += 1 if i >= len(numsData): break if i >= len(numsData): break clusters += 1 zeroClusters = clusters if numsData[0] == "0": zeroClusters += 1 if numsData[0] == "1": zeroClusters += 1 ansOne = clusters * revCost + opCost ansTwo = zeroClusters * opCost print(min(ansOne, ansTwo)) else: print(0)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR WHILE NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR WHILE VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER STRING VAR NUMBER IF VAR NUMBER STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
N, x, y = input().split() x = int(x) y = int(y) array = input() the_list = array.split("1") length = len(the_list) for k in the_list: if k == "": length -= 1 if length == 0: print(0) elif x < y: print((length - 1) * x + y) else: print(length * y)
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = (int(s) for s in input().split()) str_ = input() if str_[0] == "0": number_of_zero_sequence = 1 else: number_of_zero_sequence = 0 for i in range(1, n): if str_[i] == "0" and str_[i - 1] != "0": number_of_zero_sequence += 1 ans = 0 if x > y: ans = number_of_zero_sequence * y elif number_of_zero_sequence != 0: ans = (number_of_zero_sequence - 1) * x + y print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def countIt(a): assert a count = 1 start = ord(a[0]) - ord("0") delim = 1 - start for ch in a: if ord(ch) - ord("0") == delim: count += 1 delim = 1 - delim return count, start def calcIt(f_count, start, x, y): if f_count == 1: return y if start == 0 else 0 if f_count == 2: return y cost = 0 if x < y: if f_count > 3: cost = x * (f_count // 2 - 1) f_count = 2 + f_count % 2 if f_count == 3: if start == 0: cost += x return cost + y else: if f_count > 3: cost = y * (f_count // 2 - 1) f_count = 2 + f_count % 2 if f_count == 3: return cost + y if start == 1 else cost + 2 * y if f_count == 2: return cost + y return cost + y if start == 0 else cost def calc(a, x, y): f_count, start = countIt(a) return calcIt(f_count, start, x, y) def get_ints(): return [int(n) for n in input().split()] def get_floats(): return [float(n) for n in input().split()] a = get_ints() assert len(a) == 3 n, x, y = a[0], a[1], a[2] a = input() res = calc(a, x, y) print(res)
FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN VAR NUMBER BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def solve(s, x, y): z = False c = 0 for i in range(len(s)): if s[i] == "0" and not z: z = True c += 1 elif s[i] != "0" and z: z = False min_cost = float("inf") for i in range(c): min_cost = min(min_cost, i * x + (c - i) * y) return 0 if min_cost == float("inf") else min_cost _, x, y = [int(x) for x in input().strip().split(" ")] s = input() print(solve(s, x, y))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() pr = "2" s2 = "" nol = 0 ed = 0 for i in range(n): if s[i] != pr: pr = s[i] s2 += s[i] if int(s[i]) == 0: nol += 1 else: ed += 1 if y <= x or nol == 0: print(nol * y) else: print(y + max(0, nol - 1) * x)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().strip().split()) count = 0 r = input() if r[0] == "0": count = count + 1 for i in range(1, n): if r[i] == "0" and r[i - 1] != "0": count = count + 1 if count == 0: print(0) exit(0) elif y < x: print(y * count) else: print(x * (count - 1) + y)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP 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 NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
import sys bla, wort, *_ = sys.stdin.read().split("\n") n, x, y = [int(z) for z in bla.split(" ")] anz = wort[0] == "0" for a, b in zip(wort[:-1], wort[1:]): if b == "0" and a != "0": anz += 1 if anz == 0: print(0) else: print(y + min(x, y) * (anz - 1))
IMPORT ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER STRING FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR STRING VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = input().split() n = int(n) x = int(x) y = int(y) s = input() s += "@" zero = 0 one = 0 for i in range(n): if s[i] == "1" and (s[i + 1] == "0" or s[i + 1] == "@"): one += 1 if s[i] == "0" and (s[i + 1] == "1" or s[i + 1] == "@"): zero += 1 if zero == 0: print(0) else: print((zero - 1) * min(x, y) + y)
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) a = "1" + input() c = 0 for i in range(1, n + 1): if a[i] == "0" and a[i - 1] == "1": c += 1 print(min(c * y, max(0, y + (c - 1) * x)))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = input().split() n = int(n) x = int(x) y = int(y) ch = input() cnt = 0 if ch[0] == "0": cnt += 1 for i in range(1, len(ch)): if ch[i] == "0" and ch[i - 1] == "1": cnt += 1 ans = 0 if cnt > 0: ans = y cnt -= 1 if x < y: ans += x * cnt else: ans += y * cnt print(ans)
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def readline(): return map(int, input().split()) def main(): n, x, y = readline() line = input() zero_segments = line.count("10") + line.startswith("0") print((zero_segments - 1) * min(x, y) + y if zero_segments else 0) main()
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() lst = -1 cnt = 0 for i in s: if i == "1": if lst == 0: cnt += 1 lst = -1 else: lst = 0 if s[n - 1] == "0": cnt += 1 if cnt == 0: exit(print(0)) ans = 0 if x >= y: ans = y * cnt else: ans = (cnt - 1) * x + y print(ans)
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 VAR IF VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
from sys import stdin, stdout def main(): n, x, y = [int(i) for i in stdin.readline().split()] a = stdin.readline() prev = "1" count = 0 for i in range(n): if a[i] == "0" and prev == "1": count += 1 prev = a[i] if count == 0: print(0) else: print(y + (count - 1) * min(x, y)) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR STRING VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = list(map(int, list(input()))) if s[0] == 0: se = 1 else: se = 0 r = s[0] for i in range(1, n): if s[i] == int(not r) and r == 1: se += 1 r = 0 elif s[i] == int(not r): r = 1 k = se * y for i in range(1, se): k = min(k, (se - i) * y + i * x) print(k)
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 VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = [int(i) for i in input().split()] v = 1 s = list(input()) cnt = 0 for digit in s: if digit == "0" and v == 1: cnt += 1 v = 0 if digit == "1": v = 1 if cnt == 0: print(0) exit(0) ans = min(cnt * y, (cnt - 1) * x + y) print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
a = list(map(int, input().split())) n = a[0] x = a[1] y = a[2] s = input() c = 0 f = 0 p = s[0] b = s[0] if s[0] == "0": f = 1 for i in range(1, n): if s[i] != p: c += 1 p = s[i] if f == 0: if s[i] == "0": f = 1 e = p ans1 = c // 2 * x if f == 1: ans1 += y if b == e and b == "1" and f == 1: ans1 -= x ans2 = c // 2 * y if b != e or b != "1": ans2 += y print(min(ans1, ans2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, cost_rev, cost_inv = [int(i) for i in input().split()] a = input() lst = [] for i in range(len(a)): lst.append(int(a[i])) count = 0 flag = False for i in range(len(a)): if lst[i] == 0 and flag == False: count += 1 flag = True if lst[i] == 1: flag = False if count > 0: cost = min((count - 1) * cost_rev + cost_inv, count * cost_inv) else: cost = 0 print(cost)
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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
import sys n, x, y = map(int, input().split()) s = input() if s.count("1") == n: print(0) sys.exit() if x > y: started = False sep = 0 for c in s: if c == "0" and started: continue if c == "0" and not started: sep += 1 started = True if c == "1": started = False print(y * sep) sys.exit() started = False sep = 0 for c in s: if c == "0" and started: continue if c == "0" and not started: sep += 1 started = True if c == "1": started = False print((sep - 1) * x + y)
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR IF VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR IF VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def problem(): in1 = input() in2 = input() inp1 = list(map(int, in1.split())) n = inp1[0] exc_cost = inp1[1] flip_cost = inp1[2] s = in2 zeros = 0 sign = s[0] for num in s[1:]: if num != sign: if sign == "0": zeros += 1 sign = num if sign == "0": zeros += 1 if zeros == 0: return 0 if zeros == 1: return flip_cost cost1 = (zeros - 1) * exc_cost + flip_cost cost2 = zeros * flip_cost if cost1 <= cost2: return cost1 else: return cost2 result = problem() print(result)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() sl = [] for i in s: sl += [int(i)] sl.append(1) count = 0 for i in range(n): if sl[i] == 0 and sl[i + 1] == 1: count += 1 if count != 0: print(y + min(x, y) * (count - 1)) else: print("0")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() if "0" not in s: print(0) elif "1" not in s: print(y) else: zero_start = s[0] == "0" zero_end = s[-1] == "0" ones_groups = 0 i = 0 while i < n: found = False while i < n and s[i] == "1": found = True i += 1 if found: ones_groups += 1 i += 1 replace = ( max(0, ones_groups - (0 if zero_start else 1) - (0 if zero_end else 1)) * x + y ) invert = (ones_groups - 1 + (1 if zero_start else 0) + (1 if zero_end else 0)) * y print(min(replace, invert))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, a, b = map(int, input().split()) l = list(map(int, input())) f = True ans = 0 for t in l: if t == 0 and f: ans += 1 f = False if t == 1: f = True if ans == 0: print(0) elif b <= a: print(ans * b) else: print((ans - 1) * a + b)
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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = [int(x) for x in input().split()] s = input() c = 0 for i in range(0, len(s) - 1): if s[i] == "0" and s[i + 1] == "1": c += 1 if s[len(s) - 1] == "0": c += 1 if c == 0: print(0) else: print((c - 1) * min(x, y) + y)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) a = input() if n == 1 and a[0] == "0": print(y) elif n == 1: print("0") else: mx = 0 for i in range(n - 1): if ( a[i] == "0" and a[i + 1] == "1" or i == n - 2 and (a[i] == "0" or a[i + 1] == "0") ): mx += y mx2 = 0 change = 0 for i in range(n - 1): if a[i] == "1" and a[i + 1] == "0" and change != 0: mx2 += x if a[i] == "0": change = 1 mx2 += y print(min(mx, mx2))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def main(): n, x, y = list(map(int, input().split())) a = input() islands = a[0] == "0" for i in range(1, n): if a[i] == "0": islands += a[i - 1] == "1" if islands == 0: print(0) return 0 if x >= y: print(y * islands) else: print(x * (islands - 1) + y) return 0 main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR BIN_OP VAR NUMBER STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) s = input() c = s.count("10") if s[0] == "0": c += 1 ans = 10**18 if c == 0: print(0) else: for i in range(c): ans = min(ans, x * i + y * (c - i)) print(ans)
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 STRING VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
n, x, y = map(int, input().split()) a = input() k = 0 i = 0 if a[i] == "0": k += 1 while i < n and a[i] == "0": i += 1 for j in range(i, n - 1): if a[j] == "1" and a[j + 1] == "0": k += 1 if k == 0: print(0) elif x < y: print((k - 1) * x + y) else: print(k * y)
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 VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
[n, x, y] = [int(x) for x in input().split()] A = input() if len(A) == 1: if A[0] == "1": print("0") else: print(y) else: c = 0 if A[0] == "0": c = 1 for i in range(1, len(A)): if A[i] == "0" and A[i - 1] == "1": c += 1 m = y * c for i in range(1, c): temp = x * i + y * (c - i) if temp < m: m = temp print(m)
ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
def segments(s, x, y): count = s.count("10") + s.startswith("0") if count: return (count - 1) * min(x, y) + y return 0 n, X, Y = [int(i) for i in input().split()] t = input() print(segments(t, X, Y))
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones. Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a. You can apply the following operations any number of times: * Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»); * Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string a of length n, consisting of zeros and ones. Output Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations. Examples Input 5 1 10 01000 Output 11 Input 5 10 1 01000 Output 2 Input 7 2 3 1111111 Output 0 Note In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5]. Then the string was changed as follows: «01000» → «10000» → «11111». The total cost of operations is 1 + 10 = 11. In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5]. Then the string was changed as follows: «01000» → «11000» → «11111». The overall cost is 1 + 1 = 2. In the third example, string already consists only of ones, so the answer is 0.
inp = input().split() n = int(inp[0]) x = int(inp[1]) y = int(inp[2]) a = input().split("1") zeros = 0 for z in a: if len(z) > 0: zeros += 1 print(max(0, min(zeros * y, y + (zeros - 1) * x)))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() s = "!" + s + "!" cnt = 0 if n > 1: for char in range(ord("z"), ord("a") - 1, -1): i = 0 while len(s) > 0 and i < len(s): if i == 0: if s[i] == chr(char) and s[i] == chr(ord(s[i + 1]) + 1): s = s[:i] + s[i + 1 :] cnt += 1 i = 0 elif i == len(s) - 1: if s[i] == chr(char) and s[i] == chr(ord(s[i - 1]) + 1): s = s[:i] + s[i + 1 :] cnt += 1 i = 0 elif s[i] == chr(char) and ( s[i] == chr(ord(s[i + 1]) + 1) or s[i] == chr(ord(s[i - 1]) + 1) ): s = s[:i] + s[i + 1 :] cnt += 1 i = 0 i += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) L = list(input()) ans = 0 for i in range(25): target = chr(ord("z") - i) rm = chr(ord("z") - i - 1) update = True while update: update = False j = 0 while j < len(L): if L[j] == target: v = [] if j > 0: v.append(L[j - 1]) if j + 1 < len(L): v.append(L[j + 1]) if rm in v: L = L[:j] + L[j + 1 :] j -= 1 ans += 1 update = True j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys input = sys.stdin.readline n = int(input()) a = input() A = [100] for i in a: A.append(ord(i) - 97) A.append(100) B = [i for i in range(26, -1, -1)] for i in B: j = 1 while j < len(A) - 1: if A[j] == i: if A[j] == A[j - 1] + 1 or A[j] == A[j + 1] + 1: A.pop(j) j = 1 else: j = j + 1 else: j = j + 1 print(n - len(A) + 3)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) st = input() k = 0 while True: mx1 = -1 ar = 96 for i in range(len(st)): if i + 1 < len(st): if ord(st[i]) + 1 == ord(st[i + 1]) or ord(st[i]) - 1 == ord(st[i + 1]): if ar <= ord(st[i]): mx1 = i ar = ord(st[i]) if i - 1 >= 0: if ord(st[i]) + 1 == ord(st[i - 1]) or ord(st[i]) - 1 == ord(st[i - 1]): if ar <= ord(st[i]): mx1 = i ar = ord(st[i]) if mx1 == -1: break else: st = st[:mx1] + st[mx1 + 1 :] k += 1 print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
alphabet = "abcdefghijklmnopqrstuvwxyz" pos = {alphabet[i]: i for i in range(26)} def compute(s, n): if len(s) <= 1: return 0 char = alphabet[n] word = [] ans = 0 for i in range(len(s)): if s[i][0] == char: if ( i != 0 and pos[s[i - 1][0]] == pos[s[i][0]] - 1 or i != len(s) - 1 and pos[s[i + 1][0]] == pos[s[i][0]] - 1 ): ans += s[i][1] else: ans += compute(word, n - 1) word = [] elif not word or word[-1][0] != s[i][0]: word.append(s[i]) else: word[-1][1] += s[i][1] ans += compute(word, n - 1) return ans def simplify(s): lst = [] for i in range(len(s)): if not lst or lst[-1][0] != s[i]: lst.append([s[i], 1]) else: lst[-1][1] += 1 return lst n = input() s = simplify(input()) print(compute(s, 25))
ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST IF VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = str(input()) s = list("$" + s + "$") char = [] for i in s: char.append(ord(i)) removable = [] z = len(char) for i in range(1, n + 1): if char[i] == char[i - 1] + 1 or char[i] == char[i + 1] + 1: removable.append((char[i], i)) while len(removable) > 0: removable.sort() char.pop(removable[-1][1]) removable = [] for i in range(1, len(char) - 1): if char[i] == char[i - 1] + 1 or char[i] == char[i + 1] + 1: removable.append((char[i], i)) print(z - 2 - (len(char) - 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def f(s): if len(s) == 1 or len(s) == 0: return -1 z = -1 for i in range(len(s)): if i == 0: if ord(s[1]) - ord(s[0]) == -1: if z == -1: z = i elif s[i] > s[z]: z = i elif i == len(s) - 1: if ord(s[-2]) - ord(s[-1]) == -1: if z == -1: z = i elif s[i] > s[z]: z = i elif ord(s[i - 1]) - ord(s[i]) == -1 or ord(s[i + 1]) - ord(s[i]) == -1: if z == -1: z = i elif s[i] > s[z]: z = i return z n = int(input()) ss = input() ans = 0 x = f(ss) while x != -1: ss = ss[:x] + ss[x + 1 :] ans += 1 x = f(ss) print(ans)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input().strip() ans = 0 for i in range(26): c = chr(122 - i) f = True while f: f = False n = len(s) for i in range(n): if s[i] == c: if ( i > 0 and ord(s[i]) - ord(s[i - 1]) == 1 or i < n - 1 and ord(s[i]) - ord(s[i + 1]) == 1 ): f = True ans += 1 s = s[:i] + s[i + 1 : n] break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
ol = int(input()) s = [(ord(x) - ord("a")) for x in input()] t = sorted(s, reverse=True) for i in t: l = len(s) if l == 1: break for j in range(l): if s[j] == i: if j == 0: if abs(s[1] - i) == 1: s.pop(j) break elif j == l - 1: if abs(s[l - 2] - i) == 1: s.pop(j) break elif abs(s[j - 1] - i) == 1 or abs(s[j + 1] - i) == 1: s.pop(j) break print(ol - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = str(input()) al = "abcdefghijklmnopqrstuvwxyz" ans = 0 def p(a, j): global s, n if s[0] == a and s[1] == al[j - 1]: s = s[1:] n -= 1 return True if s[n - 1] == a and s[n - 2] == al[j - 1]: s = s[: n - 1] n -= 1 return True for i in range(1, n - 1): if s[i] == a: if s[i + 1] == al[j - 1]: s = s[:i] + s[i + 1 :] n -= 1 return True if s[i - 1] == al[j - 1]: s = s[:i] + s[i + 1 :] n -= 1 return True return False j = 25 while j > 0 and n > 1: while p(al[j], j): ans += 1 j = j - 1 if n > 1 and (s in al or s[::-1] in al): ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def main(): for _ in range(1): n = int(input()) s = tuple(ord(c) - 97 for c in input()) ans = 0 while True: b = True mx_ = -1 mx_i = -1 for i in range(n): if i > 0 and s[i] - s[i - 1] == 1: b = False if s[i] > mx_: mx_i = i mx_ = s[i] if i < n - 1 and s[i] - s[i + 1] == 1: b = False if s[i] > mx_: mx_i = i mx_ = s[i] if b: break ans += 1 n -= 1 s = s[:mx_i] + s[mx_i + 1 :] print(ans) main()
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
from sys import stdin input = stdin.buffer.readline n, s = int(input()), [(_ - ord("a")) for _ in input().rstrip()] for v in range(25, 0, -1): u = v - 1 for x in [_ for _, w in enumerate(s) if w == u]: for _ in range(x + 1, len(s)): if s[_] != v: break s[_] = -1 for _ in range(x - 1, -1, -1): if s[_] != v: break s[_] = -1 s = [_ for _ in s if _ != -1] print(n - len(s))
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input()) d = {} alp = "abcdefghijklmnopqrstuvwxyz" c = 0 for i in range(26): d[alp[i]] = i l = [] mi = d[s[0]] for i in range(n): l.append(d[s[i]]) if l[i] < mi: mi = l[i] idx = 25 while idx != 0: if len(l) == 1: break else: while True: count = 0 for i in range(len(l)): if l[i] == idx: if i == 0: if l[1] == idx - 1: l.pop(i) count += 1 break elif i == len(l) - 1: if l[len(l) - 2] == idx - 1: l.pop(i) count += 1 break elif l[i + 1] == idx - 1 or l[i - 1] == idx - 1: l.pop(i) count += 1 break if count == 0: break idx -= 1 print(n - len(l))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() for _ in range(len(s)): mx = 0 ind = -1 for i in range(len(s)): a = -1 b = -1 if i > 0: if ord(s[i - 1]) == ord(s[i]) - 1: a = i if i + 1 < len(s): if ord(s[i + 1]) == ord(s[i]) - 1: b = i if a == i or b == i: if ord(s[i]) > mx: mx = ord(s[i]) ind = i if ind == -1: break else: s = s[:ind] + s[ind + 1 :] print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() res = 0 for _ in range(n): max_char, max_index = "A", -1 for i in range(len(s)): is_ok = False if i > 0 and ord(s[i]) == ord(s[i - 1]) + 1: is_ok = True if i < len(s) - 1 and ord(s[i]) == ord(s[i + 1]) + 1: is_ok = True if is_ok and s[i] >= max_char: max_index = i max_char = s[i] if max_index > -1: s = s[:max_index] + s[max_index + 1 :] res += 1 else: break print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = [ord(i) for i in input()] for mx in range(122, 96, -1): i = 0 while i < len(s): flag = 1 if s[i] == mx: if i < len(s) - 1: if s[i + 1] == s[i] - 1: j = i while s[j] == mx: del s[j] j -= 1 i -= 1 if j == -1: break i += 1 flag = 0 if i: if s[i - 1] == s[i] - 1 and flag: j = i while s[j] == mx: del s[j] if j == len(s): break flag = 0 if flag: i += 1 print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def calc(i): if i != 0 and ord(s[i - 1]) == ord(s[i]) - 1: return True if i != len(s) - 1 and ord(s[i + 1]) == ord(s[i]) - 1: return True return False n = int(input()) s = list(input()) ans = 0 for i in range(122, 96, -1): j = 0 while j < len(s): if ord(s[j]) == i and calc(j): ans += 1 s.pop(j) j = max(0, j - 1) else: j += 1 print(ans)
FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys input = lambda: sys.stdin.readline().rstrip() n = int(input()) a = [100] + [(ord(i) - 97) for i in input()] + [100] alp = [i for i in range(26, -1, -1)] for i in alp: j = 1 while j < len(a) - 1: if a[j] == i: if a[j] == a[j - 1] + 1 or a[j] == a[j + 1] + 1: a.pop(j) j = 1 else: j += 1 else: j += 1 print(n + 2 - len(a))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
from sys import stdin input = stdin.readline n = int(input()) s = list(input().strip()) for i in range(26): char = chr(ord("z") - i) prev = chr(ord("z") - i - 1) updated = True while updated: updated = False for idx in range(len(s) - 1, -1, -1): if s[idx] == char: if idx < len(s) - 1 and s[idx + 1] == prev: s.pop(idx) updated = True elif idx > 0 and s[idx - 1] == prev: s.pop(idx) updated = True print(n - len(s))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() s = [(ord(x) - ord("a")) for x in s] cur_size = n for i in range(1, 26)[::-1]: j = cur_size - 1 while j >= 0: if s[j] != i: j -= 1 continue if j - 1 >= 0 and s[j - 1] == i - 1 or j + 1 < len(s) and s[j + 1] == i - 1: s = s[:j] + s[j + 1 :] cur_size -= 1 if j == cur_size: j -= 1 else: j -= 1 print(n - cur_size)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n, v = int(input()), 0 s = input() for _ in range(100): max_b, max_i = -1, -1 for i in range(1, n - 1): a, b, c = ord(s[i - 1]), ord(s[i]), ord(s[i + 1]) if (b == a + 1 or b == c + 1) and b > max_b: max_b, max_i = b, i if n < 2: break b, a = ord(s[0]), ord(s[1]) if b == a + 1 and b > max_b: max_b, max_i = b, 0 b, a = ord(s[n - 1]), ord(s[n - 2]) if b == a + 1 and b > max_b: max_b, max_i = b, n - 1 if max_i < 0: break else: v += 1 n -= 1 s = s[:max_i] + s[max_i + 1 :] print(v)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) actual = n s = input() for i in range(100): n = len(s) ind = -1 big = 0 for i in range(n): L = 0 R = 0 curr = ord(s[i]) if i - 1 >= 0: L = ord(s[i - 1]) if i + 1 < n: R = ord(s[i + 1]) if curr == L + 1 or curr == R + 1: if curr > big: big = curr ind = i if ind > -1: s = s[:ind] + s[ind + 1 :] print(actual - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def solve(N, S): for c in sorted(set(S), key=ord, reverse=True): temp = [] val = ord(c) for x in S: if ord(x) == val: if temp and ord(temp[-1]) == val - 1: continue if ord(x) == val - 1: while temp and ord(temp[-1]) == val: temp.pop() temp.append(x) S = temp return N - len(S) (N,) = map(int, input().split()) S = list(input()) ans = solve(N, S) print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def solve(n, s): alphabet = "zyxwvutsrqponmlkjihgfedcba" for i, c in enumerate(alphabet): if c == "a": continue nxt = alphabet[i + 1] cnt = s.count(c) for _ in range(cnt): for j, v in enumerate(s): if c != v or len(s) == 1: continue if j == 0: if s[j + 1] == nxt: s.pop(j) elif j == len(s) - 1: if s[j - 1] == nxt: s.pop(j) elif s[j + 1] == nxt or s[j - 1] == nxt: s.pop(j) print(n - len(s)) def main(): n = int(input()) s = input() solve(n, list(s)) main()
FUNC_DEF ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() ans = 0 done = True while done: done = False ind = -1 char = 0 for i in range(len(s)): if i - 1 >= 0 and ord(s[i]) - ord(s[i - 1]) == 1: done = True if ord(s[i]) > char: char = ord(s[i]) ind = i if i + 1 < len(s) and ord(s[i]) - ord(s[i + 1]) == 1: done = True if ord(s[i]) > char: char = ord(s[i]) ind = i if done: s = s[:ind] + (s[ind + 1 :] if ind + 1 < len(s) else "") ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() u = ord("a") for i in range(n): a = 0 m = 0 for j in range(len(s)): if j == 0: if j + 1 < len(s) and ord(s[j]) == ord(s[j + 1]) + 1: if ord(s[j]) > m: m = ord(s[j]) a = j elif j == len(s) - 1: if j - 1 >= 0 and ord(s[j]) == ord(s[j - 1]) + 1: if ord(s[j]) > m: m = ord(s[j]) a = j elif ( j + 1 < len(s) and ord(s[j]) == ord(s[j + 1]) + 1 or j - 1 >= 0 and ord(s[j]) == ord(s[j - 1]) + 1 ): if ord(s[j]) > m: m = ord(s[j]) a = j if m == 0: print(n - len(s)) exit() else: s = s[0:a] + s[a + 1 : len(s)]
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = input() string = input() lastString = string go = True removed = 0 def removeChar(s): index = 0 found = False for i in range(len(s)): if (ord(s[i]) > ord(s[index]) or found == False) and ( i > 0 and ord(s[i]) - ord(s[i - 1]) == 1 or i < len(s) - 1 and ord(s[i]) - ord(s[i + 1]) == 1 ): index = i found = True if found == True: s = s[:index] + s[index + 1 :] return s while go == True: string = removeChar(string) if string == lastString: go = False else: lastString = string removed += 1 print(removed)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() alf = "abcdefghijklmnopqrstuvwxyz" nums = [[] for i in range(26)] for i in range(n): num = alf.index(s[i]) nums[num].append(i) res = 0 e_n_r = {} e_n_l = {} que = [] used = [0] * n def f(k): global res, que, e_n_r, e_n_l, used, nums, alf, s, n i = alf.index(s[k]) if used[k] != 0: return ng = [k - 1, k + 1] if e_n_r.get(k) is not None: ng[1] = e_n_r[k] if e_n_l.get(k) is not None: ng[0] = e_n_l[k] if ng[0] in nums[i - 1] or ng[1] in nums[i - 1]: res += 1 used[k] = 1 e_n_r[ng[0]] = ng[1] e_n_l[ng[1]] = ng[0] if ( 0 <= ng[0] < n and alf.index(s[ng[0]]) == alf.index(s[k]) and used[ng[0]] == 0 ): que = [ng[0]] + que if ( 0 <= ng[1] < n and alf.index(s[ng[1]]) == alf.index(s[k]) and used[ng[1]] == 0 ): que = [ng[1]] + que for i in range(25, 0, -1): for k in nums[i]: que.append(k) while len(que) > 0: f(que.pop(0)) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER RETURN ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR IF NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() ans = 0 maxs = max(s) now = 0 while ord(maxs) > ord("a"): pos = s.find(maxs, now) if pos != -1: if ( pos - 1 >= 0 and ord(s[pos - 1]) == ord(maxs) - 1 or pos + 1 < len(s) and ord(s[pos + 1]) == ord(maxs) - 1 ): s = s[0:pos] + s[pos + 1 : len(s)] now = 0 ans += 1 else: now = pos + 1 else: maxs = chr(ord(maxs) - 1) now = 0 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) string = input() arr = list(string) count = 0 if n == 1: print("0") else: for i in range(122, 97, -1): lis = [] counter = 0 j = 0 while j < len(arr): if len(arr) == 1: break if ord(arr[j]) == i: if j == 0: if ord(arr[j + 1]) == i - 1: count += 1 arr.pop(j) if j > 0: j = j - 2 else: j = j - 1 elif j == len(arr) - 1: if ord(arr[j - 1]) == i - 1: count += 1 arr.pop(j) if j > 0: j = j - 2 else: j = j - 1 elif ord(arr[j + 1]) == i - 1 or ord(arr[j - 1]) == i - 1: count += 1 arr.pop(j) if j > 0: j = j - 2 else: j = j - 1 j += 1 print(n - len(arr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys input = sys.stdin.readline n = int(input()) s = [*map(ord, input().rstrip())] v = [(x, i) for i, x in enumerate(s)] v.sort() ans = 0 i = n - 1 while i >= 0: u, id = v[i] if 1 <= id and s[id - 1] == u - 1 or id + 1 < len(s) and s[id + 1] == u - 1: s.pop(id) v = sorted((x, i) for i, x in enumerate(s)) i = len(s) - 1 ans += 1 else: i -= 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR IF NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
from sys import gettrace, stdin if not gettrace(): def input(): return next(stdin)[:-1] def main(): _ = input() ss = [(ord(s) - ord("a")) for s in input()] res = 0 for c in range(26, -1, -1): found = True while c in ss and found: found = False i = 0 while i < len(ss): if ss[i] == c and ( i > 0 and ss[i - 1] == c - 1 or i < len(ss) - 1 and ss[i + 1] == c - 1 ): res += 1 del ss[i] found = True i += 1 print(res) main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def pc(c): return chr(ord(c) - 1) strlen = int(input()) string = input() removalcount = 0 azindex = {chr(96 + 26 - n): [] for n in range(26)} for n, c in enumerate(string): azindex[c].append(n) change = True while change: change = False for letter, indices in azindex.items(): if len(indices) == 0 or letter == "a": continue possible = -1 for index in indices: if string[max(index - 1, 0)] == pc(letter) or string[ min(index + 1, len(string) - 1) ] == pc(letter): string = string[:index] + string[index + 1 :] possible = index change = True removalcount += 1 break if possible > -1: for letter in azindex.keys(): newindices = [] indices = azindex[letter] for index in indices: if index > possible: newindices.append(index - 1) elif index < possible: newindices.append(index) azindex[letter] = newindices break print(removalcount)
FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() for i in range(121, 96, -1): s1 = True while s1: s1 = False if len(s) == 1: break for j in range(len(s)): if j == 0: if s[j] == chr(i) and s[j + 1] == chr(i + 1): s = s[: j + 1] + s[j + 2 :] s1 = True break elif j == len(s) - 1: if s[j] == chr(i) and s[j - 1] == chr(i + 1): s = s[: j - 1] + s[j:] s1 = True break else: if s[j] == chr(i) and s[j + 1] == chr(i + 1): s = s[: j + 1] + s[j + 2 :] s1 = True break if s[j] == chr(i) and s[j - 1] == chr(i + 1): s = s[: j - 1] + s[j:] s1 = True break print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() s = list(s) for i in range(97 + 26, 97, -1): c = chr(i) k = [] for j in range(len(s)): if j - 1 >= 0 and s[j - 1] == chr(i - 1) and s[j] == c: k.append(j) l = j + 1 while l < len(s) and s[l] == s[j]: k.append(l) l += 1 elif j + 1 < len(s) and s[j + 1] == chr(i - 1) and s[j] == c: k.append(j) l = j - 1 while l >= 0 and s[l] == s[j]: k.append(l) l -= 1 now = [] for j in range(len(s)): if j not in k: now.append(s[j]) s = now.copy() print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def MaxActions(strin): if strin: cleard = 0 peak = max(strin) start = strin.find(peak) while start != -1: group = 0 remember = False end = start while end < len(strin) and strin[end] == peak: end += 1 if ( start != 0 and ord(strin[start - 1]) + 1 == ord(peak) or end != len(strin) and ord(strin[end]) + 1 == ord(peak) ): cleard += end - start strin = strin[:start] + strin[end:] end = start start = strin.find(peak, end) return cleard + sum(map(MaxActions, strin.split(peak))) else: return 0 assert MaxActions("bacabcab") == 4 assert MaxActions("bcda") == 3 assert MaxActions("abbbbb") == 5 count = input() cities = input() print(MaxActions(cities))
FUNC_DEF IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string. In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation. For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$). Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. -----Input----- The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$. The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters. -----Output----- Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
t = int(input()) s = input() ans = "" count = 0 while True: flag = True j = -1 ch = 0 for i in range(len(s)): if ( i != 0 and ord(s[i - 1]) - ord(s[i]) == -1 or i != len(s) - 1 and ord(s[i + 1]) - ord(s[i]) == -1 ): if j == -1 or ord(ch) < ord(s[i]): ch = s[i] j = i flag = False if flag: break s = s[:j] + s[j + 1 : len(s)] count += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR