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

#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) 13.09
#endif

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

using vi = vector<int>;
using pii = pair<int, int>;

template <typename T> bool ckmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> bool ckmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}

#define ANSWER(x)                                                              \
  do {                                                                         \
    cout << (x) << '\n';                                                       \
    exit(0);                                                                   \
  } while (0)

enum Event { Onsite, Remote, Leisure };

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

  int n, k, t;
  cin >> n >> k >> t;

  vi pref[3];
  for (int e : {Onsite, Remote, Leisure}) {
    pref[e].resize(n + 1);
  }

  for (int i = 1; i <= n; i++) {
    char ch;
    cin >> ch;
    for (int e : {Onsite, Remote, Leisure}) {
      pref[e][i] = pref[e][i - 1] + (ch - '1' == e);
    }
  }

  auto cnt = [&](Event e, int l, int r) {
    if (l > r) {
      return 0;
    }
    return pref[e][r] - pref[e][l - 1];
  };
  auto work = [&](int l, int r) {
    return cnt(Remote, l, r) + cnt(Onsite, l, r);
  };
  auto total = [&](Event e) { return cnt(e, 1, n); };

  if (total(Onsite) <= k) {
    ANSWER(total(Leisure) + min(k, n - total(Leisure)));
  }

  auto calc = [&](int i, int j) -> int {
    int to_skip = work(i, i + t - 1) + work(j, j + t - 1) +
                  cnt(Onsite, 1, i - 1) + cnt(Onsite, j + t, n);

    if (to_skip > k) {
      return -1;
    }
    int residual = k - to_skip;

    return cnt(Leisure, 1, i - 1) + cnt(Leisure, j + t, n) +
           cnt(Onsite, 1, i - 1) + cnt(Onsite, j + t, n) +
           min(residual, cnt(Remote, 1, i - 1) + cnt(Remote, j + t, n));
  };

  int res = -1;
  for (int i = 1; i <= n; i++) {
    for (int j = i + t + 1; j + t - 1 <= n; j++) {
      res = max(res, calc(i, j));
    }
  }
  ANSWER(res);
}