题目描述

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. 不使用.h .lib文件使用DLL内的函数

    #include <windows.h> typedef int (*Func)(const char *fmt, ...); //这里声明一个函数指针,typedef 关键字是必须的,好 ...

  2. 传输层上的TCP和UDP

    参考: 知乎 传输层概述 “三次握手,四次挥手”你真的懂吗? 传输层上的TCP和UDP TCP/IP协议是一个协议簇.里面包括很多协议的,UDP只是其中的一个, 之所以命名为TCP/IP协议,因为TC ...

  3. 9.scrapy pycharm调试小技巧,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍

    pycharm调试技巧:调试时,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍 [用法]cmd命令运行:scrapy shell 网址 第一步,cmd进行一次请求: scrapy sh ...

  4. UML-基于GRASP对象设计步骤

    在OO设计建模的时候,在最后考虑系统启动时需要初始化的内容. 1.从用例开始,以下是一步步设计用例实现 处理销售 2.SSD 我们选择: makeNewSale 3.编写操作契约(复杂用例场景时) 4 ...

  5. update-help : 无法更新带有 UI 区域性 {zh-CN} 的模块“WindowsUpdateProvider”帮助: 在 HelpInfo XML 文件中检索不到 UI 区域性 zh-CN

    环境 OS: Windows10 企业版 LTSC x64 CPU: Intel i5-7500 CPU 3.4GHz PowerShell:5.1.17763.503 描述 更新powershell ...

  6. __init__.py在Python中的用处

    1.Python中的package定义很简单,其层次结构与程序所在目录的层次结构相同, 这一点与Java类似,唯一不同的地方在于,python中的package必须包含一个__init__.py的文件 ...

  7. 2. laravel 5.5 学习 过程中 遇到问题 的 链接

    关于 laravel 5.5 的文档 网络上已经太多 就不些太多重复的话了 在以后的 工作 中遇到问题的 查询到的解决方案 或者 相关文档将会具体写在这里 laravel 5.5 中文文档 https ...

  8. Python笔记_第四篇_高阶编程_检测_1.对函数进行单元检测

    1. 对函数进行单元检测: 单元检测: 作用:用来对一个函数.一个类.一个模块进行正确性校验工作. 结果: * 单元测试通过,说明我们测试函数的功能正确. * 单元测试不通过,说明函数有BUG,要么测 ...

  9. 项目常用JS方法封装--奋斗的IT青年(微信公众号)

                                                                                                        ...

  10. How to get AutoCAD Mtext content

    #region 提取一个图层上的各类元素 [CommandMethod("BlockInLayerCAD")] public void BlockInLayerCAD() { Do ...