//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef KoRoVa
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
mt19937 rng(4242);
const int max_n = 8042, inf = 1000111222;
int pr[4][max_n];
void solve() {
int n, k, t;
cin >> n >> k >> t;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
char c; cin >> c;
a[i] = c - '0';
for(int j = 1; j <= 3; j++) {
pr[j][i] = pr[j][i - 1] + (a[i] == j);
}
}
int ans = -1;
{ //home all day
int can_skip = k;
int can_skip_in_theory = 0;
int cur_ans = 0;
for (int i = 1; i <= n; i++) {
if(a[i] == 1) { can_skip--; cur_ans++; }
else if(a[i] == 2) can_skip_in_theory++;
else cur_ans++;
}
if(can_skip >= 0) {
umin(can_skip, can_skip_in_theory);
cur_ans += can_skip;
umax(ans, cur_ans);
}
}
{ // go to office
for(int come_to_office = t + 1; come_to_office <= n; come_to_office++) {
for(int leave_the_office = come_to_office; leave_the_office + t <= n; leave_the_office++) {
int l1 = come_to_office - t, r1 = come_to_office - 1;
int l2 = leave_the_office + 1, r2 = leave_the_office + t;
int skipped1 = t - (pr[3][r1] - pr[3][l1 - 1]);
int skipped2 = t - (pr[3][r2] - pr[3][l2 - 1]);
int skipped_at_home = (pr[1][l1 - 1]) + (pr[1][n] - pr[1][r2]);
// LOG(come_to_office, leave_the_office, skipped1, skipped2, skipped_at_home, skipped1 + skipped2 + skipped_at_home);
if(skipped1 + skipped2 + skipped_at_home <= k) {
int cur_ans = (pr[3][l1 - 1]) + (pr[3][n] - pr[3][r2]);
int can_skip = min(k - (skipped1 + skipped2 + skipped_at_home), (pr[2][l1 - 1]) + (pr[2][n] - pr[2][r2]));
// cout << cur_ans << '\n';
umax(ans, cur_ans + can_skip);
}
}
}
}
cout << ans << '\n';
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
}
/*
KoRoVa!
*/
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | //#pragma GCC optimize("Ofast", "unroll-loops") //#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4") #include <bits/stdc++.h> #define all(a) a.begin(),a.end() #define len(a) (int)(a.size()) #define mp make_pair #define pb push_back #define fir first #define sec second #define fi first #define se second using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef long double ld; template<typename T> bool umin(T &a, T b) { if (b < a) { a = b; return true; } return false; } template<typename T> bool umax(T &a, T b) { if (a < b) { a = b; return true; } return false; } #ifdef KoRoVa #define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false) #define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl template<class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); } #else #define DEBUG while (false) #define LOG(...) #endif mt19937 rng(4242); const int max_n = 8042, inf = 1000111222; int pr[4][max_n]; void solve() { int n, k, t; cin >> n >> k >> t; vector<int> a(n + 1); for(int i = 1; i <= n; i++) { char c; cin >> c; a[i] = c - '0'; for(int j = 1; j <= 3; j++) { pr[j][i] = pr[j][i - 1] + (a[i] == j); } } int ans = -1; { //home all day int can_skip = k; int can_skip_in_theory = 0; int cur_ans = 0; for (int i = 1; i <= n; i++) { if(a[i] == 1) { can_skip--; cur_ans++; } else if(a[i] == 2) can_skip_in_theory++; else cur_ans++; } if(can_skip >= 0) { umin(can_skip, can_skip_in_theory); cur_ans += can_skip; umax(ans, cur_ans); } } { // go to office for(int come_to_office = t + 1; come_to_office <= n; come_to_office++) { for(int leave_the_office = come_to_office; leave_the_office + t <= n; leave_the_office++) { int l1 = come_to_office - t, r1 = come_to_office - 1; int l2 = leave_the_office + 1, r2 = leave_the_office + t; int skipped1 = t - (pr[3][r1] - pr[3][l1 - 1]); int skipped2 = t - (pr[3][r2] - pr[3][l2 - 1]); int skipped_at_home = (pr[1][l1 - 1]) + (pr[1][n] - pr[1][r2]); // LOG(come_to_office, leave_the_office, skipped1, skipped2, skipped_at_home, skipped1 + skipped2 + skipped_at_home); if(skipped1 + skipped2 + skipped_at_home <= k) { int cur_ans = (pr[3][l1 - 1]) + (pr[3][n] - pr[3][r2]); int can_skip = min(k - (skipped1 + skipped2 + skipped_at_home), (pr[2][l1 - 1]) + (pr[2][n] - pr[2][r2])); // cout << cur_ans << '\n'; umax(ans, cur_ans + can_skip); } } } } cout << ans << '\n'; } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while (t--) solve(); } /* KoRoVa! */ |
English