message
stringlengths
2
23.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
97
109k
cluster
float64
0
0
__index_level_0__
int64
194
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of length n. Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets a_x points for erasing substring of length x. Vasya wants to maximize his total points, so help him with this! Input The first line contains one integer n (1 ≤ n ≤ 100) — the length of string s. The second line contains string s, consisting only of digits 0 and 1. The third line contains n integers a_1, a_2, ... a_n (1 ≤ a_i ≤ 10^9), where a_i is the number of points for erasing the substring of length i. Output Print one integer — the maximum total points Vasya can get. Examples Input 7 1101001 3 4 9 100 1 2 3 Output 109 Input 5 10101 3 10 15 15 15 Output 23 Note In the first example the optimal sequence of erasings is: 1101001 → 111001 → 11101 → 1111 → ∅. In the second example the optimal sequence of erasings is: 10101 → 1001 → 11 → ∅. Submitted Solution: ``` def rlist(): return list(map(int, input().split())) def rdi(): return int(input()) def f(q, p): if q == p: if string[q] == '0': return [(1, 0), (0, dp[0])] else: return [(0, dp[0]), (1, 0)] ll = q rr = p while ll < rr and (string[ll] == string[q] or string[rr] == string[p]): if string[ll] == string[q]: ll += 1 if string[rr] == string[p]: rr -= 1 if ll > rr and string[q] == string[p]: if string[q] == '0': return [(q - p + 1, 0), (0, dp[q - p])] else: return [(0, dp[q - p]), (q - p + 1, 0)] elif ll > rr: if string[q] == 0: return [(ll - q, dp[p - rr - 1]), (p - rr, dp[ll - q - 1])] else: return [(p - rr, dp[ll - q - 1]), (ll - q, dp[p - rr - 1])] else: dp_ans = f(ll, rr) if string[q] == string[p] == '0': return [(ll - q + p - rr + dp_ans[0][0], dp_ans[0][1]), (dp_ans[1][0], dp_ans[1][1] + dp[ll - q - 1] + dp[p - rr - 1])] elif string[q] == string[p] == '1': return [(dp_ans[0][0], dp_ans[0][1] + dp[ll - q - 1] + dp[p - rr - 1]), (ll - q + p - rr + dp_ans[1][0], dp_ans[1][1])] elif string[q] == '0': return [(ll - q + dp_ans[0][0], dp_ans[0][1] + dp[p - rr - 1]), (p - rr + dp_ans[1][0], dp_ans[1][1] + dp[ll - q - 1])] else: return [(p - rr + dp_ans[0][0], dp_ans[0][1] + dp[ll - q - 1]), (ll - q + dp_ans[1][0], dp_ans[1][1] + dp[p - rr - 1])] n = rdi() string = input() line = rlist() dp = [0] * (n + 1) for i in range(n): for j in range(0, i + 1): dp[i] = max(dp[i], dp[i - j - 1] + line[j]) ans = f(0, n - 1) print(max(dp[ans[0][0] - 1] + ans[0][1], dp[ans[1][0] - 1] + ans[1][1])) ```
instruction
0
2,500
0
5,000
No
output
1
2,500
0
5,001
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of length n. Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets a_x points for erasing substring of length x. Vasya wants to maximize his total points, so help him with this! Input The first line contains one integer n (1 ≤ n ≤ 100) — the length of string s. The second line contains string s, consisting only of digits 0 and 1. The third line contains n integers a_1, a_2, ... a_n (1 ≤ a_i ≤ 10^9), where a_i is the number of points for erasing the substring of length i. Output Print one integer — the maximum total points Vasya can get. Examples Input 7 1101001 3 4 9 100 1 2 3 Output 109 Input 5 10101 3 10 15 15 15 Output 23 Note In the first example the optimal sequence of erasings is: 1101001 → 111001 → 11101 → 1111 → ∅. In the second example the optimal sequence of erasings is: 10101 → 1001 → 11 → ∅. Submitted Solution: ``` n = int(input()) s = input() ai = list(map(int,input().split())) ar = [ai[i] for i in range(n)] for i in range(1,n): num = 0 for j in range(1,(i+1) // 2 + 1): num += ar[j-1] + ar[i-j] ar[i] = max(num,ar[i]) ar2 = [] num = 1 for i in range(1,n): if s[i] == s[i-1]: num += 1 else: ar2 += [num] num = 1 ar2 += [num] n2 = len(ar2) ar = [0] + ar ar3 = [[-1] * (n2 + 5) for i in range(n2+5)] ar4 = [[[-1] * (102) for i in range(102)] for i in range(102)] num5 = 0 def fun(i, n3, num3): if i > n3 - 1: return 0 ans = 0 num = num3 num2 = 0 for j in range(i,n3,2): num += ar2[j] if ar3[j+1][n3] == -1: ar3[j+1][n3] = fun(j+1,n3,0) ans = max(ans, ar3[j+1][n3] + ar[num] + num2) for z in range(j+3,n3,2): if ar3[j+1][z+1] == -1: ar3[j+1][z+1] = fun(j+1,z+1,0) if ar4[z+1][n3][num] == -1: ar4[z+1][n3][num] = fun(z+1,n3,num) ans = max(ans, ar3[j+1][z+1] + ar4[z+1][n3][num] + num2) if j < n3 - 1: num2 += ar[ar2[j+1]] return ans print(fun(0, n2, 0)) ```
instruction
0
2,501
0
5,002
No
output
1
2,501
0
5,003
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of length n. Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets a_x points for erasing substring of length x. Vasya wants to maximize his total points, so help him with this! Input The first line contains one integer n (1 ≤ n ≤ 100) — the length of string s. The second line contains string s, consisting only of digits 0 and 1. The third line contains n integers a_1, a_2, ... a_n (1 ≤ a_i ≤ 10^9), where a_i is the number of points for erasing the substring of length i. Output Print one integer — the maximum total points Vasya can get. Examples Input 7 1101001 3 4 9 100 1 2 3 Output 109 Input 5 10101 3 10 15 15 15 Output 23 Note In the first example the optimal sequence of erasings is: 1101001 → 111001 → 11101 → 1111 → ∅. In the second example the optimal sequence of erasings is: 10101 → 1001 → 11 → ∅. Submitted Solution: ``` # I'm ashamed for trying this import time import sys start = time.perf_counter() n = int(input()) st = input() + '$' A = [int(x) for x in input().split()] DP = {} DP[0] = 0 for s in range(1,n+1): DP[s] = max(DP[i]+A[s-i-1] for i in range(s)) lc = None count = 0 L = [] for c in st: if c != lc: if lc: L.append(count) count = 0 count += 1 lc = c best = 0 def f(state,score=0): global best ones = sum(state[::2]) zeros = sum(state[1::2]) if score + DP[ones] + DP[zeros] <= best: if time.perf_counter() - start > 1.75: print(best) sys.exit() #print('abort') return if not state: if best < score: best = score #print(score) return for i in range(len(state)): if i == 0: nstate = state[1:] elif i == len(state)-1: nstate = state[:-1] else: nstate = state[:i-1] + [state[i-1]+state[i+1]] + state[i+2:] f(nstate, score + DP[state[i]]) f(L) print(best) ```
instruction
0
2,502
0
5,004
No
output
1
2,502
0
5,005
Provide tags and a correct Python 3 solution for this coding contest problem. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".
instruction
0
2,670
0
5,340
Tags: combinatorics, dfs and similar, graphs, math Correct Solution: ``` from os import path;import sys,time mod = int(1e9 + 7) from math import ceil, floor,gcd,log,log2 ,factorial,sqrt from collections import defaultdict ,Counter , OrderedDict , deque;from itertools import combinations,permutations from string import ascii_lowercase ,ascii_uppercase from bisect import *;from functools import reduce;from operator import mul;maxx = float('inf') I = lambda :int(sys.stdin.buffer.readline()) lint = lambda :[int(x) for x in sys.stdin.buffer.readline().split()] S = lambda: sys.stdin.readline().strip('\n') grid = lambda r :[lint() for i in range(r)] localsys = 0 start_time = time.time() #left shift --- num*(2**k) --(k - shift) nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r) def ceill(n,x): return (n+x -1 )//x T =0 def solve(): n , m , k = lint() parent = [i for i in range(n)] rank =[0]*n def fs(a): if parent[a] == a: return a parent[a] = fs(parent[a]) return parent[a] def union(x ,y): a ,b= fs(x) , fs(y) if parent[a] != parent[b]: if rank[a] < rank[b]: parent[a] = b elif rank[a] == rank[b]: rank[a]+=1 parent[b] =a else: parent[b] = a for i in range(n - k +1): l ,h = i , i+k -1 while l < h : union(l, h) #every possible pair l+=1;h-=1 for i in range(n): fs(i) print(pow(m, len(set(parent)) , mod)) def run(): if (path.exists('input.txt')): sys.stdin=open('input.txt','r') sys.stdout=open('output.txt','w') run() T = I() if T else 1 for _ in range(T): solve() if localsys: print("\n\nTime Elased :",time.time() - start_time,"seconds") ```
output
1
2,670
0
5,341
Provide tags and a correct Python 3 solution for this coding contest problem. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".
instruction
0
2,671
0
5,342
Tags: combinatorics, dfs and similar, graphs, math Correct Solution: ``` import math p = [i for i in range(2005)] def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] def merge(x, y): r1, r2 = find(x), find(y) if r1 == r2: return p[r1] = r2 def pow(a,b,p): res = 1 while(b > 0): if b%2 == 1: res = (res*a)%p a = (a*a)%p b = int(b/2) return res n,m,k = list(map(int,input().strip().split())) mod = 10 ** 9 + 7 for i in range(n): p[i] = i for i in range(n - k + 1): for j in range(k): if i + j > k + i - j - 1: break merge(i + j, k + i - j - 1) ans = 1 for i in range(n): if p[i] == i: ans *= m ans %= mod print(ans) ```
output
1
2,671
0
5,343
Provide tags and a correct Python 3 solution for this coding contest problem. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".
instruction
0
2,672
0
5,344
Tags: combinatorics, dfs and similar, graphs, math Correct Solution: ``` n,m,k=map(int,input().split()) ans=1 if(k==1 or k>n): for i in range(n): ans*=m ans=ans%1000000007 print(ans) elif(n==k): for i in range((n+1)//2): ans*=m ans=ans%1000000007 print(ans) elif(k%2==0): print(m) else: print(m*m) ```
output
1
2,672
0
5,345
Provide tags and a correct Python 3 solution for this coding contest problem. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".
instruction
0
2,673
0
5,346
Tags: combinatorics, dfs and similar, graphs, math Correct Solution: ``` import sys from math import * from fractions import gcd readints=lambda:map(int, input().strip('\n').split()) n,m,k=readints() mod = 10**9 + 7 if k==1 or k>n: # m^n res = 1 for _ in range(n): res *= m res %= mod print(res%mod) elif k==n: e = (n+1)//2 res = 1 for _ in range(e): res *= m res %= mod print(res%mod) elif k%2==0: print(m%mod) else: res = m*m res %= mod print(res) ```
output
1
2,673
0
5,347
Provide tags and a correct Python 3 solution for this coding contest problem. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".
instruction
0
2,674
0
5,348
Tags: combinatorics, dfs and similar, graphs, math Correct Solution: ``` n, m, k = map(int, input().split()) if k == 1 or k > n: print(pow(m, n, 10**9 + 7)) elif k == n: print(pow(m, (n+1)//2, 10**9 + 7)) else: print(pow(m, k%2 + 1, 10**9 + 7)) ```
output
1
2,674
0
5,349
Provide tags and a correct Python 3 solution for this coding contest problem. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".
instruction
0
2,675
0
5,350
Tags: combinatorics, dfs and similar, graphs, math Correct Solution: ``` n,m,k = map(int,input().split()) mod = 10**9 + 7 if k == 1 or k>n: print(pow(m,n,mod)) elif k == n: print(pow(m,(n+1)//2,mod)) elif k%2== 0: print(m%mod) else: print(pow(m,2,mod)) ```
output
1
2,675
0
5,351
Provide tags and a correct Python 3 solution for this coding contest problem. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".
instruction
0
2,676
0
5,352
Tags: combinatorics, dfs and similar, graphs, math Correct Solution: ``` n, m, k = map(int, input().split()) mod = 10 ** 9 + 7 if k == 1 or k > n: print(pow(m, n, mod)) elif k == n: print(pow(m, (n + 1) // 2, mod)) else: print(pow(m, k % 2 + 1, mod)) # Made By Mostafa_Khaled ```
output
1
2,676
0
5,353
Provide tags and a correct Python 3 solution for this coding contest problem. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".
instruction
0
2,677
0
5,354
Tags: combinatorics, dfs and similar, graphs, math Correct Solution: ``` mod = 1000000007 n,m,k = map(int,input().split()) if k==1 or n<k: print(pow(m,n,mod)) elif k==n: print(pow(m,(n+1)//2,mod)) elif k%2!=0: print((m*m)%mod) else: print(m) ```
output
1
2,677
0
5,355
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". Submitted Solution: ``` import math def pow(a,b,p): res = 1 while(b > 0): if b%2 == 1: res = (res*a)%p a = (a*a)%p b = int(b/2) return res n,m,k = list(map(int,input().strip().split())) p = 10 ** 9 + 7 if k > n or k == 1: print(pow(m, n, p)) elif k == n: print(pow(m, math.ceil(k/2), p)) elif n == 1: print(m) elif k % 2 == 0: print(m) else: print(pow(m,2,p)) ```
instruction
0
2,678
0
5,356
Yes
output
1
2,678
0
5,357
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". Submitted Solution: ``` import sys,os,io import math if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w") else: input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline mod = 1000000007 def power(x, y, p) : res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res n,m,k = [int(i) for i in input().split()] parent = [int(i) for i in range(n)] k-=1 prev = -1 for i in range(n): j = i while(j<=i+(k-1)//2): #print(parent) x = i+k-(j-i) if (x>=n): break #print(j,x) J = j pa = parent[x] pb = parent[j] if (pa<pb): x,j = j,x pa = parent[x] pb = parent[j] #print("pa,pb",pa,pb) while(parent[pa]!=pa): pa = parent[pa] while(parent[pb]!=pb): pb = parent[pb] if (pa!=pb): parent[pa]=pb j = J+1 cnt = 0 for i in range(n): if parent[i]==i: cnt+=1 #print(cnt,m) print(power(m,cnt,mod)) # for i in range(n): # if (i%k==0): # prev = i # for j in range(i+k,n,k): # x = j # #print("fuck",i,x) # pa = parent[x] # pb = parent[i] # while(parent[pa]!=pa): # pa = parent[pa] # while(parent[pb]!=pb): # pb = parent[pb] # if (pa!=pb): # parent[pa]=pb # x = prev+k-i%k # if (x>=n): # break # #print("fuck",i,x) # pa = parent[x] # pb = parent[i] # while(parent[pa]!=pa): # pa = parent[pa] # while(parent[pb]!=pb): # pb = parent[pb] # if (pa!=pb): # parent[pa]=pb # #print(parent) # #print(len(set(parent))) ```
instruction
0
2,679
0
5,358
Yes
output
1
2,679
0
5,359
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". Submitted Solution: ``` from os import path;import sys,time mod = int(1e9 + 7) from math import ceil, floor,gcd,log,log2 ,factorial,sqrt from collections import defaultdict ,Counter , OrderedDict , deque;from itertools import combinations,permutations # from string import ascii_lowercase ,ascii_uppercase from bisect import *;from functools import reduce;from operator import mul;maxx = float('inf') I = lambda :int(sys.stdin.buffer.readline()) lint = lambda :[int(x) for x in sys.stdin.buffer.readline().split()] S = lambda: sys.stdin.readline().strip('\n') grid = lambda r :[lint() for i in range(r)] localsys = 0 start_time = time.time() #left shift --- num*(2**k) --(k - shift) nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r) def ceill(n,x): return (n+x -1 )//x T =0 def solve(): n , m , k = lint() if k > n or k < 2: return print((m **n)%mod) elif n == k : return print( (m ** ((n+1)//2))%mod) print(m*m) if k % 2 else print(m) def run(): if (path.exists('input.txt')): sys.stdin=open('input.txt','r') sys.stdout=open('output.txt','w') run() T = I() if T else 1 for _ in range(T): solve() if localsys: print("\n\nTime Elased :",time.time() - start_time,"seconds") ```
instruction
0
2,680
0
5,360
Yes
output
1
2,680
0
5,361
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". Submitted Solution: ``` from sys import stdin,stdout def find(a): if par[a]==a: return a else: par[a]=find(par[a]) return par[a] def union(a,b): a,b=find(a),find(b) if a!=b: if rank[a]>rank[b]: par[b]=a elif rank[b]>rank[a]: par[a]=b else: par[a]=b rank[b]+=1 n,m,k=stdin.readline().strip().split(' ') n,m,k=int(n),int(m),int(k) par=[i for i in range(n)] rank=[0 for i in range(n)] for i in range(n-k+1): l=i;u=i+k-1 while l<u: #print(l,u) union(l,u) l+=1;u-=1 for i in range(n): find(i) us=len(set(par)) #print(par) print(pow(m,us,1000000007)) ```
instruction
0
2,681
0
5,362
Yes
output
1
2,681
0
5,363
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". Submitted Solution: ``` import sys from math import * from fractions import gcd readints=lambda:map(int, input().strip('\n').split()) n,m,k=readints() mod = 10**9 + 7 if k==1: # m^n res = 1 for _ in range(n): res *= m res %= mod print(res) elif k%2==0: print(m%mod) else: res = m*(m-1) res %= mod print(res) ```
instruction
0
2,682
0
5,364
No
output
1
2,682
0
5,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". Submitted Solution: ``` n,m,k = map(int,input().split()) mod = 10**9 + 7 if k%2== 0: if k%2 == 0: print(m%mod) else: print(pow(m,k-1,mod)) ```
instruction
0
2,683
0
5,366
No
output
1
2,683
0
5,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". Submitted Solution: ``` n,k,m=map(int,input().split()) ans=1 if(k==1): for i in range(n): ans*=m ans=ans%1000000007 print(ans) elif(k%2==0): print(m) else: print(m*m) ```
instruction
0
2,684
0
5,368
No
output
1
2,684
0
5,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". Submitted Solution: ``` n,m,k=map(eval,input().split()) r = 0 if k>n or k == 1: r = m**n elif k == n: r = m**((n+1)/2) elif k%2 == 0: r = m else: r = m*m print (int(r%(10**9+7))) ```
instruction
0
2,685
0
5,370
No
output
1
2,685
0
5,371
Provide tags and a correct Python 3 solution for this coding contest problem. The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring. String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r. Help the bear cope with the given problem. Input The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters. Output Print a single number — the answer to the problem. Examples Input bearbtear Output 6 Input bearaabearc Output 20 Note In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9). In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
instruction
0
2,795
0
5,590
Tags: brute force, greedy, implementation, math, strings Correct Solution: ``` s = input() n = len(s) ans = 0 lb = -1 for i in range(n - 3): if s[i:i + 4] == 'bear': left = i-lb right = n - 3 - i ans += left * right lb = i print(ans) ```
output
1
2,795
0
5,591
Provide tags and a correct Python 3 solution for this coding contest problem. The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring. String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r. Help the bear cope with the given problem. Input The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters. Output Print a single number — the answer to the problem. Examples Input bearbtear Output 6 Input bearaabearc Output 20 Note In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9). In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
instruction
0
2,796
0
5,592
Tags: brute force, greedy, implementation, math, strings Correct Solution: ``` s=input() ans=0 w='bear' cnt=s.count(w) for i in range(cnt): ans+=(s.index(w)+1)*(len(s)-(s.index(w)+3)) s=s[s.index(w)+1:] print(ans) ```
output
1
2,796
0
5,593
Provide tags and a correct Python 3 solution for this coding contest problem. The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring. String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r. Help the bear cope with the given problem. Input The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters. Output Print a single number — the answer to the problem. Examples Input bearbtear Output 6 Input bearaabearc Output 20 Note In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9). In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
instruction
0
2,797
0
5,594
Tags: brute force, greedy, implementation, math, strings Correct Solution: ``` #B. Bear and Strings s = input() ans = 0 for i in range(len(s)) : part = s [i : ] if 'bear' in part : ans += len(part) - (part.index('bear') + 3 ) print(ans) ```
output
1
2,797
0
5,595
Provide tags and a correct Python 3 solution for this coding contest problem. The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring. String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r. Help the bear cope with the given problem. Input The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters. Output Print a single number — the answer to the problem. Examples Input bearbtear Output 6 Input bearaabearc Output 20 Note In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9). In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
instruction
0
2,798
0
5,596
Tags: brute force, greedy, implementation, math, strings Correct Solution: ``` def solution(): s = input() l = s.split('bear') if len(l) == 1: return 0 ans = 0 sums = [] _sum = 0 for i in range(len(l)-1): _sum += len(l[i]) if i>=1: _sum +=4 sums.append(_sum) for i in range(len(l)-1): if i==0: ans+=(sums[i]+1)*(len(s)-sums[i]-4+1) else: ans+=(sums[i]-sums[i-1])*(len(s)-sums[i]-4+1) return ans print(solution()) ```
output
1
2,798
0
5,597
Provide tags and a correct Python 3 solution for this coding contest problem. The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring. String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r. Help the bear cope with the given problem. Input The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters. Output Print a single number — the answer to the problem. Examples Input bearbtear Output 6 Input bearaabearc Output 20 Note In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9). In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
instruction
0
2,799
0
5,598
Tags: brute force, greedy, implementation, math, strings Correct Solution: ``` z=list(map(str,input())) x=['b','e','a','r'] a=0 ans=0 for i in range(len(z)-3): a+=1 if z[i:i+4]==x: ans+=(a*(len(z)-(i+4))+a) a=0 print(ans) ```
output
1
2,799
0
5,599
Provide tags and a correct Python 3 solution for this coding contest problem. The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring. String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r. Help the bear cope with the given problem. Input The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters. Output Print a single number — the answer to the problem. Examples Input bearbtear Output 6 Input bearaabearc Output 20 Note In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9). In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
instruction
0
2,800
0
5,600
Tags: brute force, greedy, implementation, math, strings Correct Solution: ``` s,ans=input(),0 for i in range(len(s)): f=s.find("bear",i) if f!=-1: ans+=len(s)-f-3 print(ans) ```
output
1
2,800
0
5,601
Provide tags and a correct Python 3 solution for this coding contest problem. The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring. String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r. Help the bear cope with the given problem. Input The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters. Output Print a single number — the answer to the problem. Examples Input bearbtear Output 6 Input bearaabearc Output 20 Note In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9). In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
instruction
0
2,801
0
5,602
Tags: brute force, greedy, implementation, math, strings Correct Solution: ``` from math import ceil s=input();n=len(s);ans=0 last_idx=-1 for i in range(n-3): if s[i:i+4]=='bear': if i==0:ans+=(n-1)-(i+3)+1;last_idx=i else: k=i-last_idx-1 k1=(n-1)-(i+3)+1 ans+=k*k1 ans+=k1 last_idx=i print(ans) ```
output
1
2,801
0
5,603
Provide tags and a correct Python 3 solution for this coding contest problem. The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring. String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r. Help the bear cope with the given problem. Input The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters. Output Print a single number — the answer to the problem. Examples Input bearbtear Output 6 Input bearaabearc Output 20 Note In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9). In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
instruction
0
2,802
0
5,604
Tags: brute force, greedy, implementation, math, strings Correct Solution: ``` s=input() if(len(s)<=3): print(0) else: n=len(s) ans=0 A=0 for i in range(3,n): if(s[i-3]+s[i-2]+s[i-1]+s[i]=='bear'): ans+=((i-3)-A+1)*(n-i) A=i-2 print(ans) ```
output
1
2,802
0
5,605
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string S of length n with each character being one of the first m lowercase English letters. Calculate how many different strings T of length n composed from the first m lowercase English letters exist such that the length of LCS (longest common subsequence) between S and T is n - 1. Recall that LCS of two strings S and T is the longest string C such that C both in S and T as a subsequence. Input The first line contains two numbers n and m denoting the length of string S and number of first English lowercase characters forming the character set for strings (1 ≤ n ≤ 100 000, 2 ≤ m ≤ 26). The second line contains string S. Output Print the only line containing the answer. Examples Input 3 3 aaa Output 6 Input 3 3 aab Output 11 Input 1 2 a Output 1 Input 10 9 abacadefgh Output 789 Note For the first sample, the 6 possible strings T are: aab, aac, aba, aca, baa, caa. For the second sample, the 11 possible strings T are: aaa, aac, aba, abb, abc, aca, acb, baa, bab, caa, cab. For the third sample, the only possible string T is b.
instruction
0
2,901
0
5,802
Tags: dp, greedy Correct Solution: ``` n, m = map(int, input().split()) s = input() p = c = 0 for i in range(1, n): if s[i] == s[i - 1]: c += n * (m - 1) p = i elif s[i] != s[i - 2]: p = i - 1 c += i - p ans = n * n * (m - 1) - c print(ans) ```
output
1
2,901
0
5,803
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string S of length n with each character being one of the first m lowercase English letters. Calculate how many different strings T of length n composed from the first m lowercase English letters exist such that the length of LCS (longest common subsequence) between S and T is n - 1. Recall that LCS of two strings S and T is the longest string C such that C both in S and T as a subsequence. Input The first line contains two numbers n and m denoting the length of string S and number of first English lowercase characters forming the character set for strings (1 ≤ n ≤ 100 000, 2 ≤ m ≤ 26). The second line contains string S. Output Print the only line containing the answer. Examples Input 3 3 aaa Output 6 Input 3 3 aab Output 11 Input 1 2 a Output 1 Input 10 9 abacadefgh Output 789 Note For the first sample, the 6 possible strings T are: aab, aac, aba, aca, baa, caa. For the second sample, the 11 possible strings T are: aaa, aac, aba, abb, abc, aca, acb, baa, bab, caa, cab. For the third sample, the only possible string T is b.
instruction
0
2,902
0
5,804
Tags: dp, greedy Correct Solution: ``` def main(): n, m = map(int, input().split()) s = input() k = sum(s[i] != s[i - 1] for i in range(1, n)) + 1 x = i = 0 while i < n - 1: if s[i] != s[i + 1]: j = i while i + 2 < n and s[i] == s[i + 2]: i += 1 j = (i - j) + 2 x += j * (j - 1) // 2 i += 1 ans = k * (m * n - n) - x print(ans) main() ```
output
1
2,902
0
5,805
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S of length n with each character being one of the first m lowercase English letters. Calculate how many different strings T of length n composed from the first m lowercase English letters exist such that the length of LCS (longest common subsequence) between S and T is n - 1. Recall that LCS of two strings S and T is the longest string C such that C both in S and T as a subsequence. Input The first line contains two numbers n and m denoting the length of string S and number of first English lowercase characters forming the character set for strings (1 ≤ n ≤ 100 000, 2 ≤ m ≤ 26). The second line contains string S. Output Print the only line containing the answer. Examples Input 3 3 aaa Output 6 Input 3 3 aab Output 11 Input 1 2 a Output 1 Input 10 9 abacadefgh Output 789 Note For the first sample, the 6 possible strings T are: aab, aac, aba, aca, baa, caa. For the second sample, the 11 possible strings T are: aaa, aac, aba, abb, abc, aca, acb, baa, bab, caa, cab. For the third sample, the only possible string T is b. Submitted Solution: ``` n,m=map(int,input().split()) s=input() v=0 c=s[0] j=1 t=[] front=0 p='#' for i in range(1,len(s)): x=s[i] if c==x: j+=1 else: if p==x and j==1: v-=1 else:p=c c=x t+=[j] j=1 front+=1 t+=[j] v+=len(t)*((n-1)*m+(m-1)-(n-1)) v-=front coll=n-1-front print(v) ```
instruction
0
2,903
0
5,806
No
output
1
2,903
0
5,807
Provide tags and a correct Python 3 solution for this coding contest problem. In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick. He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. Input The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. Output Print the string that satisfies all the constraints. If there are multiple answers, print any of them. Examples Input 2 Output aa Input 3 Output bba Note A palindrome is a sequence of characters which reads the same backward and forward.
instruction
0
3,004
0
6,008
Tags: constructive algorithms Correct Solution: ``` import sys multistring = (2 * (10 ** 5)) // 4 + 1 s = "aabb" * multistring n = int(input()) print(s[0:n]) ```
output
1
3,004
0
6,009
Provide tags and a correct Python 3 solution for this coding contest problem. In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick. He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. Input The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. Output Print the string that satisfies all the constraints. If there are multiple answers, print any of them. Examples Input 2 Output aa Input 3 Output bba Note A palindrome is a sequence of characters which reads the same backward and forward.
instruction
0
3,005
0
6,010
Tags: constructive algorithms Correct Solution: ``` l = int(input()) s = 'bbaa' *( (l+1)//2) print (s[:l]) ```
output
1
3,005
0
6,011
Provide tags and a correct Python 3 solution for this coding contest problem. 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.
instruction
0
3,076
0
6,152
Tags: brute force, greedy, implementation, math Correct Solution: ``` def main(): n, x, y = map(int, input().split()) s = input() p = '' t = [] for c in s: if c != p: t.append(c) p = c k = 0 for c in t: if c == '0': k += 1 if k == 0: print(0) elif k == 1: print (y) else: if x < y: print(x * (k - 1) + y) else: print(k * y) main() ```
output
1
3,076
0
6,153
Provide tags and a correct Python 3 solution for this coding contest problem. 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.
instruction
0
3,077
0
6,154
Tags: brute force, greedy, implementation, math Correct Solution: ``` def main(): n, x, y = map(int, input().split()) s, zo, a = input(), [0, 0], ' ' for b in s: if a != b: zo[b == '1'] += 1 a = b z, o = zo if s[0] == '1': o -= 1 if s[-1] == '1': o -= 1 if o < 0: o = 0 print(min(z * y, o * x + y)) if __name__ == '__main__': main() ```
output
1
3,077
0
6,155
Provide tags and a correct Python 3 solution for this coding contest problem. 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.
instruction
0
3,078
0
6,156
Tags: brute force, greedy, implementation, math Correct Solution: ``` n, x, y = list(map(int, input().split())) s = "" len_s = 0 a = input() for i in range(n): if len_s == 0: s += a[i] len_s += 1 elif a[i] != s[len_s - 1]: s += a[i] len_s += 1 sm = 0 if x < y: if len(s) > 3: sm += x * ((len(s) - 2)//2) else: if len(s) > 3: sm += y * ((len(s) - 2)//2) sm += y if len(s) != 1 and s[0] == '0' and s[len(s) - 1] == '0': if y < x: sm += y else: sm += x if s == '1': sm = 0 print(sm) ```
output
1
3,078
0
6,157
Provide tags and a correct Python 3 solution for this coding contest problem. 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.
instruction
0
3,079
0
6,158
Tags: brute force, greedy, implementation, math Correct Solution: ``` n, x, y = list(map(int,input().strip().split(' '))) s = input() groups = 0 if s[0] == '0': groups += 1 for b in range(1,n): if s[b] == '0' and s[b-1] == '1': groups += 1 if groups == 0: print(0) else: print((groups - 1) * min(x,y) + y) ```
output
1
3,079
0
6,159
Provide tags and a correct Python 3 solution for this coding contest problem. 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.
instruction
0
3,080
0
6,160
Tags: brute force, greedy, implementation, math Correct Solution: ``` n,x,y = map(int,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) ```
output
1
3,080
0
6,161
Provide tags and a correct Python 3 solution for this coding contest problem. 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.
instruction
0
3,081
0
6,162
Tags: brute force, greedy, implementation, math Correct Solution: ``` 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) ```
output
1
3,081
0
6,163
Provide tags and a correct Python 3 solution for this coding contest problem. 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.
instruction
0
3,082
0
6,164
Tags: brute force, greedy, implementation, math Correct Solution: ``` n , x , y = map(int , input().split()) s = input() c = 0 for i in range(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 ) ```
output
1
3,082
0
6,165
Provide tags and a correct Python 3 solution for this coding contest problem. 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.
instruction
0
3,083
0
6,166
Tags: brute force, greedy, implementation, math Correct Solution: ``` n, reverse, change = map(int, input().split()) a = input() g = list(filter(lambda x: x, a.split('1'))) nGroups = len(g) print(0 if not nGroups else min((nGroups - 1) * reverse + change, nGroups * change)) ```
output
1
3,083
0
6,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Submitted Solution: ``` n, x, y = tuple(map(int, input().split())) str = input() num = 0 i = 0 while i < len(str): if str[i] == '0': j = i + 1 num += 1 while j < len(str) and str[j] == '0': j += 1 i = j i += 1 if n*'1' == str: print(0) else: if x <= y: print((num-1)*x+y) else: print(num*y) ```
instruction
0
3,084
0
6,168
Yes
output
1
3,084
0
6,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Submitted Solution: ``` 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 else: if tp == 0: tp = -1 if x >= y: print(count*y) else: if count >= 2: print((count -1)*x + y) elif count == 1 : print(y) else: print(0) ```
instruction
0
3,085
0
6,170
Yes
output
1
3,085
0
6,171
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Submitted Solution: ``` n, x, y = (int (i) for i in input ().split()) s = input () on = False c = 0 for e in s: if (e == '0' and not on): on = True c += 1 elif (e == '1'): on = False print (max(0, min((c-1)*x + y, c*y))) ```
instruction
0
3,086
0
6,172
Yes
output
1
3,086
0
6,173
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Submitted Solution: ``` n, x, y = [int(i) for i in input().split()] s = [int(i) for i in input()] lis = [] ind = -1 flag0 = flag1 = 0 for i in range(n): if s[i] == 0 and flag0 == 0: ind = i flag0 = 1 flag1 = 0 else: if s[i] == 1 and flag1 == 0: if ind != -1: lis.append(ind) flag1 = 1 flag0 = 0 if flag1 == 0 and flag0 == 1: if ind != -1: lis.append(ind) x1 = len(lis) if x1 == 0: print(0) else: ans = (x1 - 1) * (min(x, y)) + y print(ans) ```
instruction
0
3,087
0
6,174
Yes
output
1
3,087
0
6,175
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Submitted Solution: ``` print(50) ```
instruction
0
3,088
0
6,176
No
output
1
3,088
0
6,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Submitted Solution: ``` """ Code of Ayush Tiwari Codeforces: servermonk Codechef: ayush572000 """ # import sys # input = sys.stdin.buffer.readline def solution(): n,x,y=map(int,input().split()) s=input() flag=0 cnt=0 for i in range(0,n-1): if s[i]=='0' and s[i+1]=='1': cnt+=1 if s[i]=='1' and s[i+1]=='0': cnt+=1 if '0' in s and cnt==0: cnt=1 # print(cnt) if cnt==0: print(0) else: if x<y: print((cnt-1)*x+y) else: print((cnt)*y) solution() ```
instruction
0
3,089
0
6,178
No
output
1
3,089
0
6,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Submitted Solution: ``` n,x,y=map(int,input().split()) A=list(input()) c=0 cur=A[0] for i in A: if i!=cur: c+=1 cur=i if x*(c-1)+y<=y*c: print(x*(c-1)+y) else: print(y*c) ```
instruction
0
3,090
0
6,180
No
output
1
3,090
0
6,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Submitted Solution: ``` a,b,c=map(int,input().split()) n=list(input()) n=n[::-1] l=0 w=0 p=min(b,c) i=0 while n[i]=="0": i+=1 w=1 if i==a: break while i<a: if n[i]=="0": w+=1 l+=p while n[i]=="0": i+=1 if i==a: break else: if n[i]=="1": while n[i]=="1": i+=1 if i==a: break #print(l,i) l+=c print(l) ```
instruction
0
3,091
0
6,182
No
output
1
3,091
0
6,183
Provide tags and a correct Python 3 solution for this coding contest problem. You are given three integers a, b and x. Your task is to construct a binary string s of length n = a + b such that there are exactly a zeroes, exactly b ones and exactly x indices i (where 1 ≤ i < n) such that s_i ≠ s_{i + 1}. It is guaranteed that the answer always exists. For example, for the string "01010" there are four indices i such that 1 ≤ i < n and s_i ≠ s_{i + 1} (i = 1, 2, 3, 4). For the string "111001" there are two such indices i (i = 3, 5). Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1. Input The first line of the input contains three integers a, b and x (1 ≤ a, b ≤ 100, 1 ≤ x < a + b). Output Print only one string s, where s is any binary string satisfying conditions described above. It is guaranteed that the answer always exists. Examples Input 2 2 1 Output 1100 Input 3 3 3 Output 101100 Input 5 3 6 Output 01010100 Note All possible answers for the first example: * 1100; * 0011. All possible answers for the second example: * 110100; * 101100; * 110010; * 100110; * 011001; * 001101; * 010011; * 001011.
instruction
0
3,327
0
6,654
Tags: constructive algorithms Correct Solution: ``` def binary_string(a, b, x): s1 = '01' s2 = '10' if x % 2 == 0 and a > b: return s1 * (x // 2) + '1' * (b - x // 2) + '0' * (a - x // 2) elif x % 2 == 0 and a <= b: return s2 * (x // 2) + '0' * (a - x // 2) + '1' * (b - x // 2) elif x % 2 != 0 and a > b: return s1 * (x // 2) + '0' * (a - x // 2) + '1' * (b - x // 2) elif x % 2 != 0 and a <= b: return s2 * (x // 2) + '1' * (b - x // 2) + '0' * (a - x // 2) A, B, X = [int(j) for j in input().split()] print(binary_string(A, B, X)) ```
output
1
3,327
0
6,655
Provide tags and a correct Python 3 solution for this coding contest problem. You are given three integers a, b and x. Your task is to construct a binary string s of length n = a + b such that there are exactly a zeroes, exactly b ones and exactly x indices i (where 1 ≤ i < n) such that s_i ≠ s_{i + 1}. It is guaranteed that the answer always exists. For example, for the string "01010" there are four indices i such that 1 ≤ i < n and s_i ≠ s_{i + 1} (i = 1, 2, 3, 4). For the string "111001" there are two such indices i (i = 3, 5). Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1. Input The first line of the input contains three integers a, b and x (1 ≤ a, b ≤ 100, 1 ≤ x < a + b). Output Print only one string s, where s is any binary string satisfying conditions described above. It is guaranteed that the answer always exists. Examples Input 2 2 1 Output 1100 Input 3 3 3 Output 101100 Input 5 3 6 Output 01010100 Note All possible answers for the first example: * 1100; * 0011. All possible answers for the second example: * 110100; * 101100; * 110010; * 100110; * 011001; * 001101; * 010011; * 001011.
instruction
0
3,328
0
6,656
Tags: constructive algorithms Correct Solution: ``` a,b,x = map(int,input().split(' ')) if b>a: if x%2!=0: s = '10'*(x-x//2) s = '1'*(b-(x//2)-1)+s+'0'*(a-(x//2)-1) else: s = '10'*((x-1)-(x-1)//2) s = s+'0'*(a-(x//2))+'1'*(b-(x//2)) else: if x%2!=0: s = '01'*(x-x//2) s = '0'*(a-(x//2)-1)+s+'1'*(b-(x//2)-1) else: s = '01'*((x-1)-(x-1)//2) s = s+'1'*(b-(x//2))+'0'*(a-(x//2)) print(s) ```
output
1
3,328
0
6,657