题目描述

There are N squares aligned in a row. The i-th square from the left contains an integer ai.

Initially, all the squares are white. Snuke will perform the following operation some number of times:

Select K consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares are overwritten.
After Snuke finishes performing the operation, the score will be calculated as the sum of the integers contained in the black squares. Find the maximum possible score.

Constraints
1≤N≤105
1≤K≤N
ai is an integer.
|ai|≤109

输入

The input is given from Standard Input in the following format:

N K
a1 a2 … aN

输出

Print the maximum possible score.

样例输入

5 3
-10 10 -10 10 -10

样例输出

10

提示

Paint the following squares black: the second, third and fourth squares from the left.Contiguous Repainting

除了最后一次刷,再这一k段序列的其他正数都可以取到

把这k段序列当作刷白的中转站,反正就算黑了几个再把这k个刷白。

最后再考虑这k个要不要刷黑

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+;
ll s[maxn],a[maxn];
int main(){
int n,k,t;
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>t;
s[i]=s[i-]+t;
a[i]=a[i-];
if(t>) a[i]+=t;
}
ll ans=;
for(int i=;i+k-<=n;i++){
ll sum=a[n]-(a[i+k-]-a[i-]);
if(s[i+k-]-s[i-]>) sum+=s[i+k-]-s[i-];
ans=max(ans,sum);
}
cout<<ans<<endl;
return ;
}

Contiguous Repainting的更多相关文章

  1. AtCoder Grand Contest 008

    AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...

  2. A@[G!C]%008

    A@[G!C]%008 A Simple Calculator 细节题. B Contiguous Repainting 最后只要有连续\(K\)个鸽子同色就可以构造方案,枚举+前缀和 C Tetro ...

  3. 【AtCoder】AGC008

    AGC008 A - Simple Calculator 如果符号相同,那么如果y比x大直接走,否则需要两次反号 如果符号不同,需要绝对值的差加一次反号 如果有一个是0,且y比x要小,那只需要一次反号 ...

  4. [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大

    17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...

  5. 论文阅读之 DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation

    DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation Xia ...

  6. Find longest contiguous sub array

    It's still an Amazon interview question. Given an array containing only stars '*' and hashes '#' . F ...

  7. [LeetCode] Contiguous Array 邻近数组

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  8. [Swift]LeetCode525. 连续数组 | Contiguous Array

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  9. 994.Contiguous Array 邻近数组

    描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...

随机推荐

  1. 19 01 03 css 中 reset 模块 设置

    主要就是让到时候 打入代码时候  把一些bug去除   或者 让一些固有的格式取消 /* 将标签默认的间距设为0 */ body,p,h1,h2,h3,h4,h5,h6,ul,dl,dt,form,i ...

  2. 吴裕雄--天生自然 JAVASCRIPT开发学习:Array(数组) 对象

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 框架-Spring及组件概念

    1.什么是Spring Spring框架是一款开源java平台.创建于2003年,轻量级框架(基本版本只有2M). 使用Spring优点: (1)     使用POJOs开发,不再需要EJB容器:如果 ...

  4. Java--Excel操作

    public static List<Info> readXml(String fileName, Map<String, Fuck> pcMap) throws Except ...

  5. 和我一起从0学算法(C语言版)(二)

    第一章 排序 第三节 快速排序 快速排序是最常用的排序方法.快排运用的递归方法很有意思.掌握了这种排序方法可以在将来学习递归时更快入门.只是快排的思路与之前的排序方法相比较为复杂,再加担心上我的表达能 ...

  6. 测网速 fping Linux查看网络即时网速 linux性能问题(CPU,内存,磁盘I/O,网络)

    Linux查看网络即时网速 fping 是ping 工具的加强版本 例出局域网中存活的主机 (Ubuntu apt-get装上  cnetos装不上) zzx@zzx11:~$ fping -a 19 ...

  7. matlab初级

    命令 ======== 系统命令 命令 功能 例 date 显示当前日期 ans = 20-Jul-2019 what 当前文件夹下的matlab文件   type 文件中的内容 type CV.m ...

  8. JAVA初学者——算数运算符

    Hello!大家好,我是浩宇大熊猫,又是学习java的一天,开开森森~ 运算符:也就是对常量或者变量进行操作的符号 表达式:用运算符把常量或者变量连接起来符合java语法的式子就可以称为表达式,不同的 ...

  9. LocalStorage基础知识小结

    cookie中每条cookie的存储空间为4k,localStorage中一般浏览器支持的是5M大小,这个在不同的浏览器中localStorage会有所不同. localStorage的写入,loca ...

  10. geodjango七日学习笔记 (7.30整理本地笔记上传到网络)

    第一天进行到现在,在开端的尾巴,想起来写一个学习笔记, 开发环境已搭好,用的是pycharm 环境是本机已有的interpreter python3.7   接下来要做的是新建一个geodjango项 ...