CF1691C.Sum of Substrings

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a binary string ss of length nn .

Let's define did_i as the number whose decimal representation is sisi+1s_i s_{i+1} (possibly, with a leading zero). We define f(s)f(s) to be the sum of all the valid did_i . In other words, f(s)=i=1n1dif(s) = \sum\limits_{i=1}^{n-1} d_i .

For example, for the string s=1011s = 1011 :

  • d1=10d_1 = 10 (ten);
  • d2=01d_2 = 01 (one)
  • d3=11d_3 = 11 (eleven);
  • f(s)=10+01+11=22f(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)f(s) that can be achieved if at most kk operations are allowed.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt ( 1t1051 \le t \le 10^5 ). Description of the test cases follows.

First line of each test case contains two integers nn and kk ( 2n1052 \le n \le 10^5 , 0k1090 \le k \le 10^9 ) — 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 10510^5 .

输出格式

For each test case, print the minimum value of f(s)f(s) you can obtain with at most kk 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 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 .
首页