Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int int64_t
- #define F(i, n) for(int i = 0; i < n; ++ i)
- int n, T, cnt = 0, lst_dom;
- string str;
- vector<int> v;
- int check(int m) {
- string s = str;
- int ans2 = INT_MAX;
- int ans = 0, rem = cnt, tec = cnt - m;
- F(i, n) {
- if(!rem) break;
- if(s[i] == 'D') {
- if(!tec) {
- int pos = max(v[m - 1], lst_dom);
- ans2 = ans + (pos - i) * 2;
- for(int j = i + 1; j <= v[m - 1]; ++ j) {
- if(s[j] == 'M') {
- ++ tec;
- s[j] = '.';
- }
- if(s[j] == 'D') {
- -- tec;
- s[j] = '.';
- -- rem;
- }
- ans += 2;
- }
- }
- -- tec;
- -- rem;
- }
- if(str[i] == 'M') ++ tec;
- if(!rem) break;
- ++ ans;
- }
- return min(ans, ans2);
- }
- int32_t main() {
- ios_base::sync_with_stdio(false);
- cin >> n >> T >> str;
- str = '.' + str; ++ n;
- F(i, n) {
- if(str[i] == 'M') v.push_back(i);
- if(str[i] == 'D') {
- ++ cnt;
- lst_dom = i;
- }
- }
- int l = 0, r = v.size() + 1;
- while(l < r) {
- int m = (l + r) / 2;
- if(check(m) <= T) {
- l = m + 1;
- } else r = m;
- }
- if(!l) {
- cout << -1;
- return 0;
- }
- cout << cnt - l + 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement