CF144B. Saving the City

B. Saving the City

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Bertown is a city with nn buildings in a straight line.

The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length nn, where the ii-th character is "1" if there is a mine under the building number ii and "0" otherwise.

Bertown's best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered xx is activated, it explodes and activates two adjacent mines under the buildings numbered x−1x−1 and x+1x+1 (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes aa coins. He can repeat this operation as many times as you want.

Also, a sapper can place a mine under a building if it wasn't there. For such an operation, he takes bb coins. He can also repeat this operation as many times as you want.

The sapper can carry out operations in any order.

You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.

Input

The first line contains one positive integer tt (1≤t≤1051≤t≤105) — the number of test cases. Then tt test cases follow.

Each test case begins with a line containing two integers aa and bb (1≤a,b≤10001≤a,b≤1000) — the cost of activating and placing one mine, respectively.

The next line contains a map of mines in the city — a string consisting of zeros and ones.

The sum of the string lengths for all test cases does not exceed 105105.

Output

For each test case, output one integer — the minimum number of coins that the sapper will have to pay.

Example

input

Copy

2
1 1
01000010
5 1
01101110

output

Copy

2
6

Note

In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, b=1b=1 coin for placing a mine and a=5a=5 coins for activating

题目大意是将给定的所有字符串中的1变成0,有两种方式,一种是将连续的1转化为0花费为a,另一种是将0转化为1花费为b

刚看这道题目的时候以为是直接贪心,但是直接贪心是做不出来的,涉及了动态规划的思想,你每次都要考虑要不要将遇到的0转化为1,解题思路就是将遇到的第一段连续的1直接加a,然后再遇见下一段1前,比较将这两段1中的0全部转化为1的花费和直接将第二段连续的1转化为0的花费a的最小值,迭代到底就可以了。

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int t;
    cin>>t;
    char str[100010];
    while(t--)
    {
        int a,b;
        cin>>a>>b;
        cin>>str;
        int len=strlen(str);
        int cnt0=0; //记录每两段1中间零的个数
        int ans=0;
        int flag=0; //判断第一次出现1
        for(int i=0;i<len;i++)
        {
            if(str[i]=='1')
            {
                if(!flag){
                    flag=1;
                    ans+=a;
                }
                else
                    ans+=min(cnt0*b,a);   //动态规划
                cnt0=0;    //归零
            }
            else if(str[i]=='0'){
                cnt0++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值