A8489 | Optimal Sum
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
And here goes another problem on arrays. You are given positive integer $len$ and array $a$ which consists of $n$ integers $a_{1}$ , $a_{2}$ , ..., $a_{n}$ . Let's introduce two characteristics for the given array.
- Let's consider an arbitrary interval of the array with length $len$ , starting in position $i$ . Value , is the modular sum on the chosen interval. In other words, the modular sum is the sum of integers on the chosen interval with length $len$ , taken in its absolute value.
- Value  is the optimal sum of the array. In other words, the optimal sum of an array is the maximum of all modular sums on various intervals of array with length $len$ .
Your task is to calculate the optimal sum of the given array $a$ . However, before you do the calculations, you are allowed to produce no more than $k$ consecutive operations of the following form with this array: one operation means taking an arbitrary number from array $a_{i}$ and multiply it by -1. In other words, no more than $k$ times you are allowed to take an arbitrary number $a_{i}$ from the array and replace it with $-a_{i}$ . Each number of the array is allowed to choose an arbitrary number of times.
Your task is to calculate the maximum possible optimal sum of the array after at most $k$ operations described above are completed.
- Let's consider an arbitrary interval of the array with length $len$ , starting in position $i$ . Value , is the modular sum on the chosen interval. In other words, the modular sum is the sum of integers on the chosen interval with length $len$ , taken in its absolute value.
- Value  is the optimal sum of the array. In other words, the optimal sum of an array is the maximum of all modular sums on various intervals of array with length $len$ .
Your task is to calculate the optimal sum of the given array $a$ . However, before you do the calculations, you are allowed to produce no more than $k$ consecutive operations of the following form with this array: one operation means taking an arbitrary number from array $a_{i}$ and multiply it by -1. In other words, no more than $k$ times you are allowed to take an arbitrary number $a_{i}$ from the array and replace it with $-a_{i}$ . Each number of the array is allowed to choose an arbitrary number of times.
Your task is to calculate the maximum possible optimal sum of the array after at most $k$ operations described above are completed.
输入格式
The first line contains two integers $n$ , $len$ ( $1<=len<=n<=10^{5}$ ) — the number of elements in the array and the length of the chosen subinterval of the array, correspondingly.
The second line contains a sequence consisting of $n$ integers $a_{1}$ , $a_{2}$ , ..., $a_{n}$ $(|a_{i}|<=10^{9})$ — the original array.
The third line contains a single integer $k$ ( $0<=k<=n$ ) — the maximum allowed number of operations.
All numbers in lines are separated by a single space.
The second line contains a sequence consisting of $n$ integers $a_{1}$ , $a_{2}$ , ..., $a_{n}$ $(|a_{i}|<=10^{9})$ — the original array.
The third line contains a single integer $k$ ( $0<=k<=n$ ) — the maximum allowed number of operations.
All numbers in lines are separated by a single space.
输出格式
In a single line print the maximum possible optimal sum after no more than $k$ acceptable operations are fulfilled.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
输入输出样例
输入 #1
5 3 0 -2 3 -5 1 2
输出 #1
10
输入 #2
5 2 1 -3 -10 4 1 3
输出 #2
14
输入 #3
3 3 -2 -5 4 1
输出 #3
11
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted