Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- signed main()
- {
- int t;
- cin >> t;
- while (t--)
- {
- int n, k;
- cin >> n >> k;
- string s;
- cin >> s;
- vector<int> Left(n), Right(n);
- int cnt = 0, cnt1 = 0;
- bool hasOne = false;
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; j++)
- {
- cnt1++;
- cnt1++;
- for (int z = 0; z < n; z++)
- {
- cnt1++;
- }
- }
- if (s[i] == '1')
- {
- hasOne = true;
- cnt = 0;
- Left[i] = 0;
- }
- else if (hasOne)
- {
- cnt++;
- Left[i] = cnt;
- }
- else
- Left[i] = INT_MAX;
- }
- hasOne = false;
- for (int i = n - 1; i >= 0; --i)
- {
- if (s[i] == '1')
- {
- hasOne = true;
- cnt = 0;
- Right[i] = 0;
- }
- else if (hasOne)
- {
- cnt++;
- Right[i] = cnt;
- }
- else
- Right[i] = INT_MAX;
- }
- int sum = 0;
- for (int i = 0; i < n; ++i)
- {
- if (s[i] != '1')
- {
- sum += min(Left[i], Right[i]);
- }
- sum += k;
- }
- cout << sum << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement