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
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n,k,t;
    cin>>n>>k>>t;

    string s;
    cin>>s;
    vector<int> pref_s(n+1,0), pref_o(n+1,0);
    for (int i=1; i<=n; i++) {
        pref_s[i] = pref_s[i-1] + (s[i-1] == '1');
        pref_o[i] = pref_o[i-1] + (s[i-1] == '2');
    }
    int req = pref_o[n] + pref_s[n] - k;

    int res = -1;
    for (int st=t; st<n-t; st++) for (int en=st; en<=n-t; en++) {
        int hm_prev = st-t, hm_nx = en+t;
        int in_office = pref_s[en]-pref_s[st] + pref_o[en]-pref_o[st];
        int from_home = 0;
        if (in_office < req) {
            int possible = pref_o[hm_prev] + pref_o[n]-pref_o[hm_nx];
            if (possible + in_office < req) continue;
            from_home = req - in_office;
        }

        int time_home = hm_prev + n-hm_nx;
        res = max(res, time_home-from_home);
    }

    if (pref_o[n] >= req) res = max(res, n-max(req,0));

    cout<<res<<'\n';

    return 0;
}