#pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; const int oo = 1e9; void cmax(int& a, int b){ a = max(a,b); } const int N = 8010; int dp[N][N][2]; int main(){ cin.tie(NULL),cin.sync_with_stdio(false); int n,k,t; cin >> n >> k >> t; string s; cin >> s; vi pref(n+1); rep(i,0,n) pref[i+1] = pref[i] + (s[i] != '3'); rep(i,0,n+1) rep(j,0,k+1) rep(l,0,2) dp[i][j][l] = -oo; dp[0][0][0] = 0; rep(i,0,n){ rep(j,0,k+1){ // stay at home int to = j + (s[i] != '3'); if (to <= k) cmax(dp[i+1][to][0], dp[i][j][0] + 1); to = j + (s[i] == '1'); if (to <= k) cmax(dp[i+1][to][0], dp[i][j][0]); // go to work or come home if (i+1>=t){ int need = pref[i+1] - pref[i+1-t]; if (need + j <= k){ cmax(dp[i+1][need + j][1], dp[i+1-t][j][0]); cmax(dp[i+1][need + j][0], dp[i+1-t][j][1]); } } // stay at work cmax(dp[i+1][j][1],dp[i][j][1]); } } int ans = -oo; rep(miss,0,k+1) cmax(ans, dp[n][miss][0]); if (ans < 0){ cout << "-1\n"; }else{ cout << ans << '\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 58 59 60 61 62 63 64 65 | #pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; const int oo = 1e9; void cmax(int& a, int b){ a = max(a,b); } const int N = 8010; int dp[N][N][2]; int main(){ cin.tie(NULL),cin.sync_with_stdio(false); int n,k,t; cin >> n >> k >> t; string s; cin >> s; vi pref(n+1); rep(i,0,n) pref[i+1] = pref[i] + (s[i] != '3'); rep(i,0,n+1) rep(j,0,k+1) rep(l,0,2) dp[i][j][l] = -oo; dp[0][0][0] = 0; rep(i,0,n){ rep(j,0,k+1){ // stay at home int to = j + (s[i] != '3'); if (to <= k) cmax(dp[i+1][to][0], dp[i][j][0] + 1); to = j + (s[i] == '1'); if (to <= k) cmax(dp[i+1][to][0], dp[i][j][0]); // go to work or come home if (i+1>=t){ int need = pref[i+1] - pref[i+1-t]; if (need + j <= k){ cmax(dp[i+1][need + j][1], dp[i+1-t][j][0]); cmax(dp[i+1][need + j][0], dp[i+1-t][j][1]); } } // stay at work cmax(dp[i+1][j][1],dp[i][j][1]); } } int ans = -oo; rep(miss,0,k+1) cmax(ans, dp[n][miss][0]); if (ans < 0){ cout << "-1\n"; }else{ cout << ans << '\n'; } } |