#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;
const int mod = 998244353;
const int N = 1e4;
int add(int a, int b)
{
return a + b < mod ? a + b : a + b - mod;
}
int sub(int a, int b)
{
return a - b >= 0 ? a - b : a - b + mod;
}
int mult(int a, int b)
{
return (LL)a * b % mod;
}
int binpow(int a, LL n)
{
int res = 1;
while (n)
{
if (n & 1)
res = mult(res, a);
a = mult(a, a);
n /= 2;
}
return res;
}
template<typename T>
void updMin(T& a, T b)
{
a = min(a, b);
}
template<typename T>
void updMax(T& a, T b)
{
a = max(a, b);
}
int cnt[N][4];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k, t;
string s;
cin >> n >> k >> t >> s;
FOR(i, 0, n)
{
FOR(j, 1, 4)
{
cnt[i + 1][j] = cnt[i][j];
}
cnt[i + 1][s[i] - '0']++;
}
int ans = -1;
if (cnt[n][1] <= k)
{
ans = cnt[n][3] + min(cnt[n][1] + cnt[n][2], k);
}
FOR(i, 0, n - 2 * t + 1)
{
FOR(j, i + t, n - t + 1)
{
int m1 = cnt[i + t][1] + cnt[n][1] - cnt[j][1];
int m2 = cnt[i + t][2] - cnt[i][2] + cnt[j + t][2] - cnt[j][2];
if (m1 + m2 <= k)
{
int c1 = cnt[i][1] + cnt[n][1] - cnt[j + t][1];
int c2 = cnt[i][2] + cnt[n][2] - cnt[j + t][2];
int c3 = cnt[i][3] + cnt[n][3] - cnt[j + t][3];
updMax(ans, c3 + c1 + min(c2, k - (m1 + m2)));
}
}
}
cout << ans << "\n";
return 0;
}
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 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--) #define SZ(a) int(a.size()) #define ALL(a) a.begin(), a.end() #define PB push_back #define MP make_pair #define F first #define S second typedef long long LL; typedef vector<int> VI; typedef vector<LL> VL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef double db; const int mod = 998244353; const int N = 1e4; int add(int a, int b) { return a + b < mod ? a + b : a + b - mod; } int sub(int a, int b) { return a - b >= 0 ? a - b : a - b + mod; } int mult(int a, int b) { return (LL)a * b % mod; } int binpow(int a, LL n) { int res = 1; while (n) { if (n & 1) res = mult(res, a); a = mult(a, a); n /= 2; } return res; } template<typename T> void updMin(T& a, T b) { a = min(a, b); } template<typename T> void updMax(T& a, T b) { a = max(a, b); } int cnt[N][4]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k, t; string s; cin >> n >> k >> t >> s; FOR(i, 0, n) { FOR(j, 1, 4) { cnt[i + 1][j] = cnt[i][j]; } cnt[i + 1][s[i] - '0']++; } int ans = -1; if (cnt[n][1] <= k) { ans = cnt[n][3] + min(cnt[n][1] + cnt[n][2], k); } FOR(i, 0, n - 2 * t + 1) { FOR(j, i + t, n - t + 1) { int m1 = cnt[i + t][1] + cnt[n][1] - cnt[j][1]; int m2 = cnt[i + t][2] - cnt[i][2] + cnt[j + t][2] - cnt[j][2]; if (m1 + m2 <= k) { int c1 = cnt[i][1] + cnt[n][1] - cnt[j + t][1]; int c2 = cnt[i][2] + cnt[n][2] - cnt[j + t][2]; int c3 = cnt[i][3] + cnt[n][3] - cnt[j + t][3]; updMax(ans, c3 + c1 + min(c2, k - (m1 + m2))); } } } cout << ans << "\n"; return 0; } |
English