CF1691C.Sum of Substrings
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a binary string s of length n .
Let's define di as the number whose decimal representation is sisi+1 (possibly, with a leading zero). We define f(s) to be the sum of all the valid di . In other words, f(s)=i=1∑n−1di .
For example, for the string s=1011 :
- d1=10 (ten);
- d2=01 (one)
- d3=11 (eleven);
- f(s)=10+01+11=22 .
In one operation you can swap any two adjacent elements of the string. Find the minimum value of f(s) that can be achieved if at most k operations are allowed.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤105 ). Description of the test cases follows.
First line of each test case contains two integers n and k ( 2≤n≤105 , 0≤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 s of length n , consisting of only zeros and ones.
It is also given that sum of n over all the test cases doesn't exceed 105 .
输出格式
For each test case, print the minimum value of f(s) you can obtain with at most k operations.
输入输出样例
输入#1
3 4 0 1010 7 1 0010100 5 2 00110
输出#1
21 22 12
说明/提示
- For the first example, you can't do any operation so the optimal string is s itself. f(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 f value of 22 .
- For the third example, one of the optimal strings you can obtain is "00011". The string has an f value of 12 .