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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 8e3 + 3;
int biuro[MAXN], zdalne[MAXN];
int n,k,t;

int oblicz(int a, int b){
    int op = 0; //opuszczone

    if(a>0) op += biuro[a-1];
    //cout << "   "<<  op << " ";
    if(a>0){
        op += biuro[a+t-1]-biuro[a-1];  op += zdalne[a+t-1]-zdalne[a-1];
    }
    else
        op += biuro[a+t-1]+zdalne[a+t-1];
    //cout << op << " ";
    op += biuro[b+t-1]-biuro[b-1]; op += zdalne[b+t-1]-zdalne[b-1];
    //cout << op << " ";
    op += biuro[n-1] - biuro[b+t-1];
    //cout << op << "    ";

    if(op>k) return -1;

    int zad = 0, spotkania=0;
    zad = n - (b-a+t);
    //cout << zad << " ";
    if(a>0) spotkania += zdalne[a-1];
    spotkania += zdalne[n-1]-zdalne[b+t-1];

    zad -= max(spotkania-(k-op),0);
    return zad;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> k >> t;
    string s; cin >> s;
    biuro[0] = (s[0]=='1');
    zdalne[0] = (s[0]=='2');
    for(int i=1;i<n;i++){
        if(s[i]=='1') biuro[i]++;
        else if(s[i]=='2') zdalne[i]++;
        biuro[i] += biuro[i-1];
        zdalne[i] += zdalne[i-1];
    }
    if(biuro[n-1]<=k){
        cout << n - max(0,(biuro[n-1]+zdalne[n-1]-k)) << '\n';
        return 0;
    }

    int result = -1;
    for(int i=0;i<n-2*t;i++){
        for(int j=i+t+1;j<n-t+1;j++){
            result = max(result, oblicz(i,j));
            //cout << i << " " << j << " " << oblicz(i,j) << '\n';
        }
    }
    cout << result;
}