Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).
Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.
Input
* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
Output
Sample Input
7 2 2 1 1 2 2 1 1
Sample Output
6
Hint
Seven apples fall – one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.
OUTPUT DETAILS:
Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 1000000007 16 #define ll long long 17 using namespace std; 18 19 int m,n; 20 int dp[1005][35]; 21 int num[1005]; 22 int main() 23 { 24 while(scanf("%d %d",&n,&m)!=EOF) 25 { 26 for(int i=1;i<=n;i++) 27 { 28 scanf("%d",&num[i]); 29 } 30 if(num[1]==1) 31 { 32 dp[1][0]=1; 33 dp[1][1]=0; 34 } 35 else 36 { 37 dp[1][0]=0; 38 dp[1][1]=1; 39 } 40 for(int i=2;i<=n;i++) 41 { 42 for(int j=0;j<=m;j++) 43 { 44 if(j==0) 45 { 46 dp[i][j]=dp[i-1][j]+num[i]%2; 47 } 48 else 49 { 50 dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]); 51 if(j%2+1==num[i]) 52 { 53 dp[i][j]++; 54 } 55 } 56 } 57 } 58 int ans=0; 59 for(int i=0;i<=m;i++) 60 { 61 ans=max(ans,dp[n][i]); 62 } 63 printf("%d\n",ans); 64 } 65 }