// Witold Milewski
// PA 2025
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i=a; i<=b; ++i)
#define FORB(i, b, a) for(int i=b; i>=a; --i)
#define sz(A) (int)(A.size())
#define eb emplace_back
#define pb push_back
#define pi pair<int, int>
#define f first
#define s second
#define rs resize
#define V vector
const int maxn=8007;
int P[6][maxn];
int S[5][maxn];
int n, k, t, w, odp=-1;
V<int> A;
signed main() {
cin.tie(0) -> ios_base::sync_with_stdio(0);
cin >> n >> k >> t;
A.rs(n+1);
string inp; cin >> inp;
FOR(i, 1, n) A[i]=inp[i-1]-'1'+1;
FOR(z, 1, 3) FOR(i, 1, n) P[z][i]=P[z][i-1]+(A[i]==z);
FOR(z, 1, 3) FORB(i, n, 1) S[z][i]=S[z][i+1]+(A[i]==z);
FOR(i, 1, n) {
P[4][i]=P[1][i]+P[3][i];
S[4][i]=S[1][i]+S[3][i];
P[5][i]=P[1][i]+P[2][i];
}
k=min(k, P[1][n]+P[2][n]);
w=P[1][n]+P[2][n]-k;
if(P[2][n]>=w) odp=max(odp, P[3][n]+P[1][n]+P[2][n]-w);
FOR(a, t+1, n-t) {
int p = a-t-1;
FOR(b, a, n-t) {
int s = b+t+1;
int x = P[5][b] - P[5][a-1];
int y = P[2][p] + S[2][s];
int cos = x+y;
if(cos>=w) {
int z = min(cos-w, y);
odp=max(odp, P[4][p] + S[4][s] + z);
}
}
}
cout << odp << '\n';
}
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 | // Witold Milewski // PA 2025 #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i=a; i<=b; ++i) #define FORB(i, b, a) for(int i=b; i>=a; --i) #define sz(A) (int)(A.size()) #define eb emplace_back #define pb push_back #define pi pair<int, int> #define f first #define s second #define rs resize #define V vector const int maxn=8007; int P[6][maxn]; int S[5][maxn]; int n, k, t, w, odp=-1; V<int> A; signed main() { cin.tie(0) -> ios_base::sync_with_stdio(0); cin >> n >> k >> t; A.rs(n+1); string inp; cin >> inp; FOR(i, 1, n) A[i]=inp[i-1]-'1'+1; FOR(z, 1, 3) FOR(i, 1, n) P[z][i]=P[z][i-1]+(A[i]==z); FOR(z, 1, 3) FORB(i, n, 1) S[z][i]=S[z][i+1]+(A[i]==z); FOR(i, 1, n) { P[4][i]=P[1][i]+P[3][i]; S[4][i]=S[1][i]+S[3][i]; P[5][i]=P[1][i]+P[2][i]; } k=min(k, P[1][n]+P[2][n]); w=P[1][n]+P[2][n]-k; if(P[2][n]>=w) odp=max(odp, P[3][n]+P[1][n]+P[2][n]-w); FOR(a, t+1, n-t) { int p = a-t-1; FOR(b, a, n-t) { int s = b+t+1; int x = P[5][b] - P[5][a-1]; int y = P[2][p] + S[2][s]; int cos = x+y; if(cos>=w) { int z = min(cos-w, y); odp=max(odp, P[4][p] + S[4][s] + z); } } } cout << odp << '\n'; } |
English