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
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

// office / remote
vector<pair<int, int>> mem[8001];

pair<int,int> subs(int i, int j) {
    if(i == j) return mem[0][0];
    return mem[j - i][i];
}

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

    int n, k, t, best;
    string s;
    cin >> n >> k >> t;
    cin >> s;
    mem[0].assign(1, make_pair(0, 0));
    mem[1].resize(n);
    for(int i = 0; i < n; ++i) {
        mem[1][i] = make_pair(s[i] == '1' ? 1 : 0, s[i] == '2' ? 1 : 0);
    }
    for(int l = 2; l <= n; ++l) {
        mem[l].resize(n-l+1);
        for(int i = 0; i <= n-l; ++i) {
            mem[l][i].first = mem[l-1][i+1].first + mem[1][i].first;
            mem[l][i].second = mem[l-1][i+1].second + mem[1][i].second;
        }
    }
    best = -1;
    if(mem[n][0].first <= k) {
        best = n - max(mem[n][0].first + mem[n][0].second - k, 0);
    }
    for(int i = t; i <= n-t; ++i) {
        auto home_left = subs(0, i-t); // [0..i-t)
        auto drive_left = subs(i-t, i); // [i-t, i)
        for(int j = i; j <= n-t; ++j) {
            auto office = subs(i, j); // [i..j)
            auto drive_right = subs(j, j+t); // [j, j+t)
            auto home_right = subs(j+t, n); // [j+t, n]
            auto home = make_pair(home_left.first + home_right.first, home_left.second + home_right.second);
            auto drive = make_pair(drive_left.first + drive_right.first, drive_left.second + drive_right.second);
            if(drive.first + drive.second + home.first > k) continue;
            int tbest = n - 2*t - (j-i) - max(home.second - (k-drive.first-drive.second-home.first), 0);
            if(tbest > best) {
                best = tbest;   
            }
        }
    }
    cout << best << endl;
    return 0;
}