Sum of Substrings solution codeforces
You are given a binary string ss of length nn.
Let’s define didi as the number whose decimal representation is sisi+1sisi+1 (possibly, with a leading zero). We define f(s)f(s) to be the sum of all the valid didi. In other words, f(s)=∑i=1n−1dif(s)=∑i=1n−1di.
For example, for the string s=1011s=1011:
- d1=10d1=10 (ten);
- d2=01d2=01 (one)
- d3=11d3=11 (eleven);
- f(s)=10+01+11=22f(s)=10+01+11=22.
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051≤t≤105). Description of the test cases follows.
First line of each test case contains two integers nn and kk (2≤n≤1052≤n≤105, ≤k≤1090≤k≤109) — the length of the string and the maximum number of operations allowed.
The second line of each test case contains the binary string ss of length nn, consisting of only zeros and ones.
It is also given that sum of nn over all the test cases doesn’t exceed 105105.
Sum of Substrings solution codeforces
Output
For each test case, print the minimum value of f(s)f(s) you can obtain with at most kk operations.
Example
Sum of Substrings solution codeforces
input
Copy
3 4 0 1010 7 1 0010100 5 2 00110
output
Copy
21 22 12
Note
Sum of Substrings solution codeforces
- For the first example, you can’t do any operation so the optimal string is ss itself. f(s)=f(1010)=10+01+10=21f(s)=f(1010)=10+01+10=21.
- For the second example, one of the optimal strings you can obtain is “0011000“. The string has an ff value of 2222.
- For the third example, one of the optimal strings you can obtain is “00011“. The string has an ff value of 1212.
SOLUTION
“Click here“

I am the Founder and the creator of the blog neoideasblog.com where we share latest trivia questions and answers on a daily basis.