B. Saving the City:题目
题意:1是炸弹,引爆的同时引爆i-1,i+1,埋一个炸弹的成本为b,引爆的成本为a
思路:首先如果有炸弹,必须引爆一次,然后往后遍历,看是引爆还是接上的成本更小就行,
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> a((int)4e5);
vector<int> b((int)4e5);
string str;
int main()
{int t;cin >> t;while (t--){ll a, b;cin >> a >> b;cin >> str;int n = str.length();ll res = 0, cou = 0, ff = -1;for (int i = 0; i < n; i++){if (str[i] == '1'){ff = i;break;}}if (ff>=0) res = a;for (int i=ff;i<n;i++){if (str[i]=='1') {res+= min(cou*b,a);cou = 0;}else cou++;}cout<<res<<endl;}
}