#include <bits/stdc++.h>
using namespace std;
#define _DEBUG
#ifdef _DEBUG
template<typename T1, typename T2> auto& operator<<(ostream& o, pair<T1, T2> a) { return o << "(" << a.first << ", " << a.second << ")"; }
template<typename T, typename OS> auto& operator<<(OS& o, T a) { o << "{"; for (auto b : a) o << b << ", "; return o << "}"; }
#define dbg(x...) cerr << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\n"; }(x)
#else
#define dbg(...)
#endif
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vi = vector<int>;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
ll n, k, t, ans = -1;
cin >> n >> k >> t;
string v; cin >> v;
vector<ll> p1(n+1), p2(n+1), p3(n+1);
for(ll i = 1 ; i <= n ; ++i) {
p1[i] = p1[i-1]; p2[i] = p2[i-1]; p3[i] = p3[i-1];
if(v[i-1] == '1') p1[i]++;
if(v[i-1] == '2') p2[i]++;
if(v[i-1] == '3') p3[i]++;
}
if(p1[n] <= k) {
ans = p3[n]+min(k, p1[n]+p2[n]);
} else {
for(ll len = 2*t+1 ; len <= n ; ++len) {
for(ll l1 = 1 ; l1+len-1 <= n ; ++l1) {
ll r1 = l1+t-1, l2 = l1+len-t, r2 = l1+len-1;
ll cnt = p1[n] - p1[l2-1] + p1[r1] +
p2[r2] - p2[l2-1] +
p2[r1] - p2[l1-1];
ll re1 = p1[n] - p1[r2] + p1[l1-1];
ll re2 = p2[n] - p2[r2] + p2[l1-1];
ll re3 = p3[n] - p3[r2] + p3[l1-1];
if(cnt <= k) {
ans = max(ans, re3 + re1 + min(k-cnt, re2));
}
}
}
}
cout << ans;
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 | #include <bits/stdc++.h> using namespace std; #define _DEBUG #ifdef _DEBUG template<typename T1, typename T2> auto& operator<<(ostream& o, pair<T1, T2> a) { return o << "(" << a.first << ", " << a.second << ")"; } template<typename T, typename OS> auto& operator<<(OS& o, T a) { o << "{"; for (auto b : a) o << b << ", "; return o << "}"; } #define dbg(x...) cerr << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\n"; }(x) #else #define dbg(...) #endif #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define F first #define S second using ll = long long; using ld = long double; using pll = pair<ll, ll>; using vi = vector<int>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll n, k, t, ans = -1; cin >> n >> k >> t; string v; cin >> v; vector<ll> p1(n+1), p2(n+1), p3(n+1); for(ll i = 1 ; i <= n ; ++i) { p1[i] = p1[i-1]; p2[i] = p2[i-1]; p3[i] = p3[i-1]; if(v[i-1] == '1') p1[i]++; if(v[i-1] == '2') p2[i]++; if(v[i-1] == '3') p3[i]++; } if(p1[n] <= k) { ans = p3[n]+min(k, p1[n]+p2[n]); } else { for(ll len = 2*t+1 ; len <= n ; ++len) { for(ll l1 = 1 ; l1+len-1 <= n ; ++l1) { ll r1 = l1+t-1, l2 = l1+len-t, r2 = l1+len-1; ll cnt = p1[n] - p1[l2-1] + p1[r1] + p2[r2] - p2[l2-1] + p2[r1] - p2[l1-1]; ll re1 = p1[n] - p1[r2] + p1[l1-1]; ll re2 = p2[n] - p2[r2] + p2[l1-1]; ll re3 = p3[n] - p3[r2] + p3[l1-1]; if(cnt <= k) { ans = max(ans, re3 + re1 + min(k-cnt, re2)); } } } } cout << ans; return 0; } |
English