#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 8e3+100;
int n, k, x, prf[N][4];
int sp[N];
void solve() {
cin>>n>>k>>x;
string s; cin>>s;
for (int i=1; i<=n; ++i) {
sp[i] = s[i-1] - '0';
for (int j=0; j<4; ++j) {
prf[i][j] = prf[i-1][j] + (sp[i] == j);
}
}
// jedzie do biura co najmniej raz
int ans = -1;
for (int l=1; l<=n; ++l) {
for (int r=l; r<=n; ++r) {
if (l-x < 1 || r+x > n) continue;
int cp = k - (prf[l-1][1] + (prf[n][1] - prf[r][1]));
cp -= (prf[l-1][2] - prf[l-x-1][2]) + (prf[r+x][2] - prf[r][2]);
if (cp < 0) continue;
int lf3 = prf[l-x-1][3] + (prf[n][3] - prf[r+x][3]);
int lf2 = min(cp, prf[l-x-1][2] + (prf[n][2] - prf[r+x][2]));
int lf1 = prf[l-x-1][1] + (prf[n][1] - prf[r+x][1]);
ans = max(ans, lf3 + lf2 + lf1);
}
}
// caly dzien w domu
int s1 = prf[n][1];
int cp = k - s1;
if (cp >= 0) {
ans = max(ans, prf[n][3] + min(cp, prf[n][2]) + prf[n][1]);
}
cout<<ans<<"\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t=1; //cin>>t;
while (t--) solve();
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 8e3+100; int n, k, x, prf[N][4]; int sp[N]; void solve() { cin>>n>>k>>x; string s; cin>>s; for (int i=1; i<=n; ++i) { sp[i] = s[i-1] - '0'; for (int j=0; j<4; ++j) { prf[i][j] = prf[i-1][j] + (sp[i] == j); } } // jedzie do biura co najmniej raz int ans = -1; for (int l=1; l<=n; ++l) { for (int r=l; r<=n; ++r) { if (l-x < 1 || r+x > n) continue; int cp = k - (prf[l-1][1] + (prf[n][1] - prf[r][1])); cp -= (prf[l-1][2] - prf[l-x-1][2]) + (prf[r+x][2] - prf[r][2]); if (cp < 0) continue; int lf3 = prf[l-x-1][3] + (prf[n][3] - prf[r+x][3]); int lf2 = min(cp, prf[l-x-1][2] + (prf[n][2] - prf[r+x][2])); int lf1 = prf[l-x-1][1] + (prf[n][1] - prf[r+x][1]); ans = max(ans, lf3 + lf2 + lf1); } } // caly dzien w domu int s1 = prf[n][1]; int cp = k - s1; if (cp >= 0) { ans = max(ans, prf[n][3] + min(cp, prf[n][2]) + prf[n][1]); } cout<<ans<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); } |
English