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

#define loop(i, a, b) for(int i = a; i <= b; i++)
#define loop_rev(i, a, b) for(int i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define eb emplace_back
#define pb push_back

using ui = uint32_t;
using ll = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, k, t; cin >> n >> k >> t;

  vector<vector<int>> s(n + 1);
  vector<int>(3).swap(s[0]);

  auto suma = [&](int a, int b, int typ) -> int {
    return s[b][typ] - s[a - 1][typ];
  };

  loop(i, 1, n) {
    s[i] = s[i - 1];
    char c; cin >> c;
    ++s[i][c - '1'];
  }

  constexpr int BIURO = 0;
  constexpr int ZDALNIE = 1;
  constexpr int LABA = 2;
  int res = (-1);

  loop(a, 1, n) {
    loop(b, a - 1, n) {
      if(a - t < 1 || b + t > n) continue;
      int skip_biuro = suma(1, n, BIURO) - suma(a, b, BIURO);
      int skip_zdalnie = suma(a - t, b + t, ZDALNIE) - suma(a, b, ZDALNIE);
      int laba = suma(1, n, LABA) - suma(a - t, b + t, LABA);
      int pozostale = (k - skip_biuro - skip_zdalnie);
      if(pozostale < 0) continue;
      res = max(
        res,
        min(pozostale, suma(1, a - t - 1, ZDALNIE) + suma(b + t + 1, n, ZDALNIE))
        + suma(1, a - t - 1, BIURO) + suma(b + t + 1, n, BIURO)
        + laba
      );
    }
  }

  if(s[n][BIURO] <= k) {
    int tres = s[n][LABA] + s[n][BIURO] + min(k - s[n][BIURO], s[n][ZDALNIE]);
    if(tres > res) {
      res = tres;
    }
  }

  cout << res << '\n';

}