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
#include <bits/stdc++.h>
using namespace std;

template <typename T, typename T2, typename T3>
struct trio{
    T first;
    T2 second;
    T3 third;

    trio operator + (const trio& other) const {
        return {first + other.first, second + other.second, third + other.third};
    }
};

typedef trio<int,int,int> int_trio;

const int N = 8e3 + 3; 

int_trio ps[N];

void read(int n){
    char a;
    ps[0] = {0, 0, 0};
    for(int i = 1; i <= n; i++){
        cin >> a;
        ps[i] = ps[i - 1] + (int_trio){(a == '1'), (a == '2'), (a == '3')};
    }
    return;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, k, t;
    cin >> n >> k >> t;
    read(n);
    int ans = -1, x, y, zp;
    for(int i = 1; i <= n - 2*t + 1; i++){
        for(int j = i + 2*t - 1; j <= n; j++){
            x = (ps[i + t - 1].first - ps[i - 1].first) + (ps[i + t - 1].second - ps[i - 1].second) + (ps[j].first - ps[j - t].first) + (ps[j].second - ps[j - t].second);
            y = (ps[i - 1].first - ps[0].first) + (ps[n].first - ps[j].first);
            if(x + y > k) continue;
            zp = (ps[i - 1].third - ps[0].third) + (ps[n].third - ps[j].third) + y + min(k - (x + y), (ps[i - 1].second - ps[0].second) + (ps[n].second - ps[j].second));
            ans = max(ans, zp);
        }
    }
    x = ps[n].first;
    if(x <= k) ans = max(ans, ps[n].third + x + min(k - x, ps[n].second));
    cout << ans << "\n";
    return 0;
}