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
#include <iostream>
#include <vector>
using namespace std;
struct dp {
  int one;
  int two;
  int three;
};
int main() {
  int n, k, t;
  int sum, max = -1, count, twos;
  int ones = 0;
  cin >> n >> k >> t;
  vector<char> tab(n);
  vector<dp> left(n), right(n);
  for (int i = 0; i < n; ++i)
    cin >> tab[i];

  for (int i = 0; i < n; ++i) {
    if (tab[i] == '1') {
      ones++;
    }
  }
  if (ones <= k) {
    k -= ones;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      if (tab[i] == '2' && k) {
        ans++;
        k--;
      } else if (tab[i] != '2') {
        ans++;
      }
    }
    cout << ans;
    return 0;
  }
  for (int i = 0, one = 0, two = 0, three = 0; i < n; ++i) {
    left[i] = {one, two, three};
    if (tab[i] == '1')
      one++;
    else if (tab[i] == '2')
      two++;
    else
      three++;
  }
  for (int i = n - 1, one = 0, two = 0, three = 0; i >= 0; --i) {
    right[i] = {one, two, three};
    if (tab[i] == '1')
      one++;
    else if (tab[i] == '2')
      two++;
    else
      three++;
  }

  for (int i = 0; i < n - 2 * t; ++i) {
    for (int j = t; j < n - i - t; ++j) {
      sum = 0;
      sum += left[j].two - left[j - t].two;
      sum += left[j].one;
      sum += right[j + i].two - right[j + i + t].two;
      sum += right[j + i].one;
      if (sum <= k) {
        count = 0;
        count += left[j - t].one + right[j + i + t].one;
        count += left[j - t].three + right[j + i + t].three;
        count += std::min(left[j - t].two + right[j + i + t].two, k - sum);
        max = std::max(max, count);
      }
    }
  }
  cout << max;
}