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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    int n,k,t,s=0;
    cin >> n >> k >> t;
    string tm;
    vector<int> g(n);
    vector<int> praca(n),zdalne(n);
    cin >> tm;
    for(int i=0;i<n;i++){
        g[i]=tm[i]-'0';
    }
    for(int i=0;i<n;i++){
        if(g[i]==2) {
            s++;
            zdalne[i]++;
        }
        if(g[i]==1) {
            s++;
            praca[i]++;
        }
        if(i>0){
            praca[i]+=praca[i-1];
            zdalne[i]+=zdalne[i-1];

        }
    }

    if(k>=s){
        //cout << "chuj";
        cout << n;
        return 0;
    }
    int wyn=-1;

    if(s-zdalne[n-1]<=k){
        int twyn=n-max(0,(s-k));
        wyn=max(twyn,wyn);
    }
    for(int i=0;i<n-2*t+1;i++){
        for(int j=i+t;j<n-t+1;j++){
            //cout << i << ' ' << j << '\n';

            int l,r;
            l=i+t;
            r=j-1;
            int twyn=0;
            int biuro = (praca[r]-praca[l-1])+(zdalne[r]-zdalne[l-1]);
            if(s-biuro<=k){
                twyn+=i;
                twyn+=(n-(j+t));
                //cout << "DONE" << ' ' << i << ' ' << j << ' ' << biuro << ' '<< twyn << '\n';
                wyn=max(twyn,wyn);
                continue;
            }
            else{
                int pzd;
                if(i>0) pzd = zdalne[i-1]+zdalne[n-1]-zdalne[r+t];
                else pzd = zdalne[n-1]-zdalne[r+t];
                int lt = ((s-biuro)-k);
                if(pzd>=lt){
                    twyn+=i;
                    twyn+=(n-(j+t));
                    twyn-=lt;
                    //cout << "NOT DONE" << ' ' << i << ' ' << j << ' ' << biuro << ' ' << pzd << ' ' << twyn << ' ' << lt << '\n';
                    //cout << biuro << '\n';
                    wyn=max(twyn,wyn);
                    continue;
                } 
            }

        }
    }

    cout << wyn;

}