# include <bits/stdc++.h>
typedef long long ll;
using namespace std;
# define For(i , l , r) for(int i = (l); i <= (r); i++)
# define Rep(i , n) For(i , 0 , (n) - 1)
# define size(x) (ll)x.size()
# define all(x) x.begin(),x.end()
# define MAXN 8002
const ll inf = 1e9 + 7;
const ll mod = 998244353;
ll n , m , k , ans = 0 , qq , t , timer = 0 , s;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> t;
string s;
cin >> s;
s = '*' + s;
ll have = 0 , have2 = 0;
vector<ll>pref(n + 5) , suf(n + 5) , pref2(n + 5);
For (i , 1 , size(s) - 1) {
pref2[i] = pref2[i - 1] + (s[i] == '1');
if (s[i] == '1' || s[i] == '2') have++;
if (s[i] == '2') have2++;
}
For (i , 1 , size(s) - 1) {
pref[i] = pref[i - 1] + (s[i] == '2');
}
for (int i = size(s) - 1; i; i--) {
suf[i] = suf[i + 1] + (s[i] == '2');
}
ans = -1;
For (i , 1 , size(s) - 1) {
For (j , i , size(s) - 1) {
if (i - t - 1 >= 0 && j + t + 1 <= size(s)) {
ll sum = pref2[j] - pref2[i - 1] +
pref[j] - pref[i - 1];
ll need = max((have - k) - sum , 0LL);
ll can = pref[i - t - 1] + suf[j + t + 1];
if (need > can) continue;
ll res = i - t - 1 + (n - (j + t)) - need;
if (ans < res) {
ans = max(ans , res);
}
}
}
}
if (have - k <= have2) {
ll need = max(0LL , have - k);
cout << n - need;
}
else {
if (ans < 0) cout << -1;
else cout << ans;
}
}
/*
*/
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 | # include <bits/stdc++.h> typedef long long ll; using namespace std; # define For(i , l , r) for(int i = (l); i <= (r); i++) # define Rep(i , n) For(i , 0 , (n) - 1) # define size(x) (ll)x.size() # define all(x) x.begin(),x.end() # define MAXN 8002 const ll inf = 1e9 + 7; const ll mod = 998244353; ll n , m , k , ans = 0 , qq , t , timer = 0 , s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> t; string s; cin >> s; s = '*' + s; ll have = 0 , have2 = 0; vector<ll>pref(n + 5) , suf(n + 5) , pref2(n + 5); For (i , 1 , size(s) - 1) { pref2[i] = pref2[i - 1] + (s[i] == '1'); if (s[i] == '1' || s[i] == '2') have++; if (s[i] == '2') have2++; } For (i , 1 , size(s) - 1) { pref[i] = pref[i - 1] + (s[i] == '2'); } for (int i = size(s) - 1; i; i--) { suf[i] = suf[i + 1] + (s[i] == '2'); } ans = -1; For (i , 1 , size(s) - 1) { For (j , i , size(s) - 1) { if (i - t - 1 >= 0 && j + t + 1 <= size(s)) { ll sum = pref2[j] - pref2[i - 1] + pref[j] - pref[i - 1]; ll need = max((have - k) - sum , 0LL); ll can = pref[i - t - 1] + suf[j + t + 1]; if (need > can) continue; ll res = i - t - 1 + (n - (j + t)) - need; if (ans < res) { ans = max(ans , res); } } } } if (have - k <= have2) { ll need = max(0LL , have - k); cout << n - need; } else { if (ans < 0) cout << -1; else cout << ans; } } /* */ |
English