Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit
Status

Description

It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must
wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.




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

* Line 1: Two space separated integers: T and W



* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.

Output

* Line 1: The maximum number of apples Bessie can catch without walking more than W times.

Sample Input

7 2
2
1
1
2
2
1
1

Sample Output

6

Hint

INPUT DETAILS:



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.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[1010][1010][2];
int num[1010];
int main()
{
int n,time;
int maxx=-1000;
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
scanf("%d%d",&n,&time);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=1;i<=n;i++)
{
dp[i][0][0]=dp[i-1][0][0]+(num[i]==1);
dp[i][0][1]=dp[i-1][0][1]+(num[i]==2);
for(int j=1;j<=time;j++)
{
dp[i][j][0]=max(dp[i-1][j-1][1],dp[i-1][j][0])+(num[i]==1);
dp[i][j][1]=max(dp[i-1][j-1][0],dp[i-1][j][1])+(num[i]==2);
maxx=max(maxx,max(dp[i][j][0],dp[i][j][1]));
}
}
printf("%d\n",maxx);
return 0;
}

最新文章

  1. 使用 GCC 和 GNU Binutils 编写能在 x86 实模式运行的 16 位代码
  2. 计算机程序的思维逻辑 (40) - 剖析HashMap
  3. 【解决】Word 在试图打开文件时遇到错误 请尝试下列方法:* xxx * xxx * xxx
  4. 一步一步教你如何解锁被盗的iPhone 6S
  5. Hibernate-入门教程
  6. 剑指 offer set 1 二维数组中查找
  7. LINUX 运维命令
  8. Linux 命令 - mkdir: 创建目录
  9. xposed系列教程
  10. Truncating HTML attribute value in SharePoint DataFormWebPart
  11. 掌众android面试题
  12. java压缩文件出现中文乱码问题
  13. C语言学习笔记-顺序表
  14. 转 shell中字分隔的妙用:变量IFS
  15. iOS bug调试技巧学习----breakpoint&amp;condition
  16. WAS应用--虚拟主机
  17. spring4——IOC之基于注解的依赖注入(DI )
  18. Buy or Build(UVa1151)
  19. Windows 10(UWP)开发技巧 - PageUserControl
  20. 【转】Cowboy 开源 WebSocket 网络库

热门文章

  1. ASP.NET-Router配置中MapRoute的参数
  2. n个骰子,和为x的概率分别是多少
  3. [Hyperapp] Interact with the State Object through Hyperapp Action functions
  4. Hibernate的xml方法配置和操作代码
  5. bzoj4868: [Shoi2017]期末考试(三分法)
  6. 如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
  7. VC6.0 设置动态链接库工程生成dll以及lib文件的位置
  8. Windows 下 Sketch 替代 APP(UWP)
  9. PC比价软件
  10. Mojo Core Embedder API