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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#include <bits/stdc++.h>

using namespace std;

const int mx = 8007;

int n, k, t, sum[mx], sumjed[mx], sumdwa[mx], res=-1;
string s;

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

    cin>>n>>k>>t;
    cin>>s;

    for (int i = 1; i <= n; i++) {
        sum[i] = sum[i-1];
        sumjed[i] = sumjed[i-1];
        sumdwa[i] = sumdwa[i-1];
        if (s[i-1]=='1') {
            sumjed[i]++;
        }
        if (s[i-1]=='2') {
            sumdwa[i]++;
        }
        if (s[i-1]=='3') {
            sum[i]++;
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = i+1; j <= n; j++) {
            int jazda=0, tempk=k, jedwdomu = 0, ilewdomu=0, ilewpracy=0, ilepotyczek=0;
            ilewpracy+=j-i+1;
            if (ilewpracy < 2*t+1) {
                res = max(res, -1);
                continue;
            }
            jazda+=sumdwa[j]-sumdwa[j-t];
            jazda+=sumdwa[i+t-1]-sumdwa[i-1];
            jazda+=sumjed[j]-sumjed[j-t];
            jazda+=sumjed[i+t-1]-sumjed[i-1];
            if (jazda > tempk) {
                res = max(res, -1);
                continue;
            }
            tempk-=jazda;
            jedwdomu+=sumjed[n]-sumjed[j];
            jedwdomu+=sumjed[i-1]-sumjed[0];
            if (jedwdomu > tempk) {
                res = max(res, -1);
                continue;
            }
            tempk-=jedwdomu;
            ilewdomu+=n-j;
            ilewdomu+=i-1;
            ilepotyczek+=sum[n]-sum[j];
            ilepotyczek+=sum[i-1]-sum[0];
            if (ilepotyczek!=ilewdomu) {
                if (tempk > (ilewdomu-ilepotyczek)) {
                    res = max(res, ilewdomu);
                    continue;
                }
                res = max(res, ilepotyczek+tempk);
            }
            else {
                res = max(res, ilepotyczek);
            }
        }
    }

    int jazda=0, tempk=k, jedwdomu = 0, ilewdomu=0, ilewpracy=0, ilepotyczek=0;
    jedwdomu+=sumjed[n];
    if (jedwdomu > tempk) {
        res = max(res, -1);
        cout<<res;
        return 0;
    }
    tempk-=jedwdomu;
    ilewdomu+=n;
    ilepotyczek+=sum[n];
    if (ilepotyczek!=ilewdomu) {
        if (tempk > (ilewdomu-ilepotyczek)) {
            res = max(res, ilewdomu);
            cout<<res;
            return 0;
        }
        res = max(res, ilepotyczek+tempk);
        cout<<res;
        return 0;
    }
    else {
        res = max(res, ilepotyczek);
        cout<<res;
        return 0;
    }

}