Ilya Muromets

Gym - 100513F

Силачом слыву недаром — семерых одним ударом!
From the Russian cartoon on the German fairy tale.

Ilya Muromets is a legendary bogatyr. Right now he is struggling against Zmej Gorynych, a dragon with n heads numbered from 1 to n from left to right.

Making one sweep of sword Ilya Muromets can cut at most k contiguous heads of Zmej Gorynych. Thereafter heads collapse getting rid of empty space between heads. So in a moment before the second sweep all the heads form a contiguous sequence again.

As we all know, dragons can breathe fire. And so does Zmej Gorynych. Each his head has a firepower. The firepower of the i-th head is fi.

Ilya Muromets has time for at most two sword sweeps. The bogatyr wants to reduce dragon's firepower as much as possible. What is the maximum total firepower of heads which Ilya can cut with at most two sword sweeps?

Input

The first line contains a pair of integer numbers n and k (1 ≤ n, k ≤ 2·105) — the number of Gorynych's heads and the maximum number of heads Ilya can cut with a single sword sweep. The second line contains the sequence of integer numbers f1, f2, ..., fn (1 ≤ fi ≤ 2000), where fi is the firepower of the i-th head.

Output

Print the required maximum total head firepower that Ilya can cut.

Examples

Input
8 21 3 3 1 2 3 11 1

Output
20

Input
4 10010 20 30 40

Output100
题意:  给你两个数n和k,然后n个数,进行两次操作,一次最多可以消除连续的K个数,然后剩余的数又变成连续的,问两次操作后,消除的数的总和最大为多少。题解:  dp[i][1]表示的是1-i的区间中,第一次操作能得到的最大值,因为i从k-n,每次加一,所以每次对最大值可能造成改变的只有新加进来的数,dp[i][1]=max(dp[i-1][1],sum[i]-sum[i-k]);  dp[i][1]表示的是1-i的区间中,两次操作后能得到的最大值,dp[i][2]=max(dp[i-1][2],sum[i]-sum[i-k]+dp[i-k][1]);对于两次操作,状态转移方程就是1- i-1区间两次操作后最大值或者是新加入的i影响的区间sum[i]-sum[i-k]+1- i-k区间操作一次的最大值。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=2e5+10;
 7 int sum[maxn],a[maxn],dp[maxn][3];
 8 int main()
 9 {
10     int n,k;
11     scanf("%d%d",&n,&k);
12     for(int i=1;i<=n;i++)
13     {
14         scanf("%d",&a[i]);
15         sum[i]=sum[i-1]+a[i];
16     }
17     if(2*k>=n)
18     {
19         printf("%d\n",sum[n]);
20         return 0;
21     }
22     memset(dp,0,sizeof(dp));
23     for(int i=k;i<=n;i++)
24     {
25         dp[i][1]=max(dp[i-1][1],sum[i]-sum[i-k]);
26         dp[i][2]=max(dp[i-1][2],sum[i]-sum[i-k]+dp[i-k][1]);
27        // printf("%d %d %d\n",i,dp[i][1],dp[i][2]);
28     }
29     printf("%d\n",dp[n][2]);
30     return 0;
31 }

 

转载于:https://www.cnblogs.com/1013star/p/10070083.html

Ilya Muromets(DP or 思维)相关推荐

  1. F - Ilya Muromets Gym - 100513F

    Силачом слыву недаром - семерых одним ударом! From the Russian cartoon on the German fairy tale. Ily ...

  2. Codeforces 1408 D. Searchlights(优化DP、思维)

    整理的算法模板合集: ACM模板 传送门 DP的思想,因为题目中有两个维度,数据达到了1e6,所以我们直接开二维数组显然不太恰当,而且我们的答案不好选取,我们可以使用一个技巧,开一维数组,用DP的下标 ...

  3. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

  4. 【51Nod - 1270】数组的最大代价(dp,思维)

    题干: 数组A包含N个元素A1, A2......AN.数组B包含N个元素B1, B2......BN.并且数组A中的每一个元素Ai,都满足1 <= Ai <= Bi.数组A的代价定义如下 ...

  5. 【CodeForces - 789C】Functions again(最大子段和变形,dp,思维)

    题干: Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian s ...

  6. 【CodeForces - 467C】George and Job(dp,思维)

    题干: The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, ...

  7. 【HDU - 4055】Number String(dp,思维)

    题干: The signature of a permutation is a string that is computed as follows: for each pair of consecu ...

  8. 【CodeForces - 1096D】Easy Problem(dp,思维)

    题目大意: 现在有一个由小写字母组成的字符串,去掉这个字符串的第i个位置的字符会有ai的代价.你的任务是去掉这个字符串中的一些字符使得该字符串中不包含子序列hard,且去掉字符的代价之和尽可能小. 输 ...

  9. 【HDU - 6567】Cotree(树形dp,思维)

    题干: Avin has two trees which are not connected. He asks you to add an edge between them to make them ...

最新文章

  1. TComboBox下拉取值
  2. bash: ./make_ext4fs: No such file or directory 错误解决方法
  3. 2021年春招Elasticsearch面试题
  4. 查询计算机专业及选修了英语的学生,实验五 数据库综合查询(学生)
  5. View的三大流程之View的测量
  6. 分块编码(Transfer-Encoding: chunked)VS Content-length
  7. 【webpack系列】从零搭建 webpack4+react 脚手架(四)
  8. (转)RabbitMQ学习之exchange总结
  9. 孪生神经网络_孪生网络如何选择负样本? 这是个很好的问题
  10. php sqrt函数,sqrt函数怎么使用
  11. POJ 2762 Going from u to v or from v to u? (判断单连通)
  12. 【译】Silverlight for Windows Phone Toolkit In Depth(五)
  13. c语言因子优化算法,【代码】求一个数的因数和、求优化、顺便也供新人参考算法...
  14. httpunit测试遭遇org.mozilla.javascript.NativeGlobal.constructError
  15. MATLAB GBK编码
  16. RapidMiner教程
  17. ES部分查询方法,elasticsearch查询方法
  18. 公司邮箱怎么用微信收邮件?企业微信邮箱原来这么方便!
  19. 一次成功的云存储接管实战
  20. R COOKBOOK 学习笔记

热门文章

  1. [JS 分析] 天_眼_查 字体文件
  2. LVS的四种模式的实现
  3. 基于opencv在摄像头ubuntu根据视频获取
  4. 世界500强高频逻辑推理智力面试题(一)
  5. 关于学力、同等学力与学历、同等学历的区别
  6. Mac安装nginx配置过程
  7. vs2005 vc++ 生成非托管的 不需要.net运行环境的exe程序方法
  8. Java03接口与内部类
  9. 交换机的基本原理配置(一)
  10. amazeui学习笔记--css(基本样式3)--文字排版Typography