1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    int k; cin >> k;
    int t; cin >> t;
    string s; cin >> s;
    s = "#"+s;
    vector<int> b(n+1), z(n+1), f(n+1);
    for (int i = 1; i <= n; i++) {
        b[i] = b[i-1] + (s[i] == '1');
        z[i] = z[i-1] + (s[i] == '2');
        f[i] = f[i-1] + (s[i] == '3');
    }
    int need = max(0, z[n] + b[n] - k);
    int ans = -1;
    if (z[n] >= need) {
        ans = n-need;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = i+2*t-1; j <= n; j++) {
            if (z[i-1] + (z[j-t]-z[i+t-1]) + (b[j-t]-b[i+t-1]) + (z[n] - z[j]) < need) continue;
            int done = (z[j-t]-z[i+t-1]) + (b[j-t]-b[i+t-1]);
            ans = max(ans, (i-1)+(n-j)-(need-done));
        }
    }
    cout << ans << '\n';
}