CodeCraft-22 and Codeforces Round 795 (Div. 2)
C
Sum of Substrings
Codeforces
1400
原题
← BD →
翻译中…

You are given a binary string $s$ of length $n$.

翻译中…

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

翻译中…

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

翻译中…
  • $d_1 = 10$ (ten);
  • $d_2 = 01$ (one)
  • $d_3 = 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.

翻译中…

Input

翻译中…

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

翻译中…

First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \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 $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 $10^5$.

翻译中…

Output

翻译中…

For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations.

翻译中…

Note

翻译中…
  • 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$.
翻译中…

样例

Input #1
3
4 0
1010
7 1
0010100
5 2
00110
Output #1
21
22
12
InputCopy OutputCopy
题目信息
题号
1691C
时间限制
1 s
内存限制
256 MB
标签
暴力
构造
贪心
数学
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。