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
79
80
81
82
83
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


const int maxx = 8e3+7;

int pre[maxx][4];
int n,k,t;

int que(int a, int b, int x){
    return pre[a+1][x] - pre[b][x];
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k>>t;
    string s; cin>>s;
    for(int i=0; n>i; i++){
        if(s[i] == '3'){
            pre[i+1][3] = 1;
        }else if(s[i] == '2'){
            pre[i+1][2] = 1;
        }else{
            pre[i+1][1] = 1;
        }
        for(int j=1; 3>=j; j++){
            pre[i+1][j] += pre[i][j];
        }
    }

    int res = -1;

    // cout<<" que(n,0,3) : "<<que(n-1,0,3)<<endl;
    // cout<<" que(n,0,2) : "<<que(n-1,0,2)<<endl;
    // cout<<" que(n,0,1) : "<<que(n-1,0,1)<<endl;

    if( k - que(n-1,0,1) >= 0){
        res = min(k,que(n-1,0,2)+que(n-1,0,1)) + que(n-1,0,3);
    }

    //cout<<"res bez pojscia do biura : "<<res<<endl;

    for(int i=t; n-t>i; i++){
        for(int j=i; n-t>j; j++){
            
            //cout<<"{ i , j } -> "<<i<<" "<<j<<endl;
            
            int lost_in_moving = que(i-1,i-t,1) + que(i-1,i-t,2) + que(j+t,j+1,1) + que(j+t,j+1,2);
            
           // cout<<"lost_in_moving : "<<lost_in_moving<<endl;
            
            int lost_out_office = 0;
            int x = 0;
            if(i-t != 0) lost_out_office += que(i-t-1,0,1);
            if(j+t != n-1) lost_out_office += que(n-1,j+t+1,1);

            //cout<<"lost_out_office : "<<lost_out_office<<endl;

            if(lost_in_moving + lost_out_office <= k){
                //cout<<"dla tego przedzialu dziala"<<endl;
                int pom = 0;
                int pom2 = 0;
                if(i-t != 0) pom += que(i-t-1,0,3);
                if(j+t != n-1) pom += que(n-1,j+t+1,3);
                pom += lost_out_office;
                if(i-t != 0) pom2 += que(i-t-1,0,2);
                if(j+t != n-1) pom2 += que(n-1,j+t+1,2);
                //cout<<"pom : "<<pom<<endl;
                pom += min(k - lost_in_moving - lost_out_office, pom2);
                //cout<<"pom2 : "<<pom2<<endl;
                res = max(pom,res);
            }
            //cout<<endl;
        }
        //cout<<endl;
    }
    cout<<res;
}