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
#include<bits/stdc++.h>
using namespace std;
constexpr int MAXN = 8002;
int n, k, t, ileZ[MAXN*2], ileB[MAXN*2];
string s;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k >> t;
  cin >> s;
  for(int i = 1; i <= n; ++i) {
    ileZ[i] = ileZ[i-1];
    ileB[i] = ileB[i-1];
    if(s[i-1] == '1') ileB[i]++;
    else if(s[i-1] == '2') ileZ[i]++;
  }
  int ans{-1};
  for(int i = 1+t; i <= n; ++i) {
    for(int j = i+1+t; j <= n+1; ++j) {
      int ileZ_lewy = ileZ[i-t-1];
      int ileB_lewy = ileB[i-t-1];
      int ileZ_prawy = ileZ[n] - ileZ[j-1];
      int ileB_prawy = ileB[n] - ileB[j-1];
      int ileZ_doBiura = ileZ[i-1] - ileZ[i-1-t];
      int ileB_doBiura = ileB[i-1] -  ileB[i-1-t];
      int ileZ_doDomu = ileZ[j-1] - ileZ[j-1-t];
      int ileB_doDomu = ileB[j-1] - ileB[j-1-t];
      if(ileB_lewy + ileB_prawy + ileB_doBiura + ileB_doDomu + ileZ_doBiura + ileZ_doDomu > k) continue;
      else {
        int skip = min(k-(ileZ_doDomu + ileB_doDomu + ileB_doBiura + ileZ_doBiura + ileB_lewy + ileB_prawy), ileZ_prawy + ileZ_lewy);
        ans = max(ans, n - (ileZ_lewy + ileZ_prawy) + skip - (j+t-i));
      }
    }
  }
  
  if(ileB[n] <= k) {
    ans = max(ans, n + min(k-ileB[n], ileZ[n]) - ileZ[n]);
  }

  cout << ans << '\n';
}