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
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k, t;
    cin >> n >> k >> t;
    string s;
    cin >> s;
    vector<int> ones(n + 1);
    vector<int> twos(n + 1);
    vector<int> free(n + 1);
    for (int i = 0; i < s.size(); i++) {
        ones[i + 1] = ones[i] + (s[i] == '1');
        twos[i + 1] = twos[i] + (s[i] == '2');
        free[i + 1] = free[i] + (s[i] == '3');
    }
    if (ones[n] <= k) {
        int res = free[n] + ones[n] + min(k - ones[n], twos[n]);
        cout << res << endl;
        return 0;
    }
    int res = -1;
    for (int i1 = t; i1 <= n - t; i1++) {
        int missed1 = ones[i1] + twos[i1] - twos[i1 - t];
        int maybe1 = twos[i1 - t];
        int f1 = free[i1 - t] + ones[i1 - t];
        if (missed1 <= k) {
            for (int i2 = i1 + t; i2 <= n; i2++) {
                int missed2 = ones[n] - ones[i2 - t] + twos[i2] - twos[i2 - t];
                if (missed1 + missed2 > k)
                    continue;
                int maybe2 = twos[n] - twos[i2];
                int f2 = free[n] - free[i2] + ones[n] - ones[i2];
                res = max(res, f1 + f2 + min(k - missed1 - missed2, maybe1 + maybe2));
            }
        }
    }
    cout << res << endl;
    return 0;
}