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

#define all(a) begin(a), end(a)
using ll = long long;

constexpr int inf = -1e9;
void solve() {
    int n = 8000, k = 1000, t = 70;
    cin >> n >> k >> t;

    vector<int> a(n);
    for (auto &i : a) {
        // i = 1 + (rand() % 3);

        char c;
        cin >> c;
        i = c - '0';
    }

    vector<int> any_work_pref(n), free_pref(n), offline_work_pref(n), online_work_pref(n);
    for (int i = 0; i < n; i++) {
        any_work_pref[i] = a[i] != 3;
        free_pref[i] = a[i] == 3;
        offline_work_pref[i] = a[i] == 1;
        online_work_pref[i] = a[i] == 1;
    }
    partial_sum(all(any_work_pref), begin(any_work_pref));
    partial_sum(all(free_pref), begin(free_pref));
    partial_sum(all(offline_work_pref), begin(offline_work_pref));
    partial_sum(all(online_work_pref), begin(online_work_pref));

    int cnt = count(all(a), 1);
    if (cnt <= k) {
        int work = count(all(a), 1) + count(all(a), 2);
        cout << n - work + min(work, k) << "\n";
        return;
    }

    auto get = [&](vector<int> const &pref, int l, int r) {
        if (l > r)
            return 0;
        return pref[r] - (l ? pref[l - 1] : 0);
    };

    int res = -1;
    for (int i = 0; i < n; i++) {
        for (int j = i + t; j + t <= n; j++) {
            int skip_road = get(any_work_pref, i, i + t - 1) + get(any_work_pref, j, j + t - 1);
            int skip_offline = get(offline_work_pref, 0, i - 1) + get(offline_work_pref, j + t, n - 1);
            int skip = skip_road + skip_offline;

            if (skip > k)
                continue;

            int can_skip_online = get(online_work_pref, 0, i - 1) + get(online_work_pref, j + t, n - 1);

            int free = get(free_pref, 0, i - 1) + get(free_pref, j + t, n - 1);
            int local = free + skip_offline + min(can_skip_online, k - skip);

            res = max(res, local);
        }
    }
    cout << res << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int tests = 1;
    // cin >> tests;

    while (tests--)
        solve();
}