D. Yet Another Subarray Problem

You are given an array \(a_1, a_2, \dots , a_n\) and two integers \(m\) and \(k\).

You can choose some subarray \(a_l, a_{l+1}, \dots, a_{r-1}, a_r\).

The cost of subarray \(a_l, a_{l+1}, \dots, a_{r-1}, a_r\) is equal to \(\sum\limits_{i=l}^{r} a_i - k \lceil \frac{r - l + 1}{m} \rceil\), where \(\lceil x \rceil\) is the least integer greater than or equal to \(x\).

The cost of empty subarray is equal to zero.

For example, if \(m = 3\), \(k = 10\) and \(a = [2, -4, 15, -3, 4, 8, 3]\), then the cost of some subarrays are:

\(a_3 \dots a_3: 15 - k \lceil \frac{1}{3} \rceil = 15 - 10 = 5\);

\(a_3 \dots a_4: (15 - 3) - k \lceil \frac{2}{3} \rceil = 12 - 10 = 2\);

\(a_3 \dots a_5: (15 - 3 + 4) - k \lceil \frac{3}{3} \rceil = 16 - 10 = 6\);

\(a_3 \dots a_6: (15 - 3 + 4 + 8) - k \lceil \frac{4}{3} \rceil = 24 - 20 = 4\);

\(a_3 \dots a_7: (15 - 3 + 4 + 8 + 3) - k \lceil \frac{5}{3} \rceil = 27 - 20 = 7\).

Your task is to find the maximum cost of some subarray (possibly empty) of array \(a\).

Input

The first line contains three integers \(n\), \(m\), and \(k\) (\(1 \le n \le 3 \cdot 10^5, 1 \le m \le 10, 1 \le k \le 10^9\)).

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)).

Output

Print the maximum cost of some subarray of array \(a\).

Examples

input

7 3 10

2 -4 15 -3 4 8 3

output

7

input

5 2 1000

-13 -4 -9 -20 -11

output

0

题目大意

给你一个长度为n的序列,然后你需要找到一个代价最大的子序列。子序列的代价是该序列的\(\sum\limits_{i=l}^{r} a_i - k \lceil \frac{r - l + 1}{m} \rceil\)

题解

实际上就是背包dp,dp[i][j]表示装第i个物品的时候,此时物品的数量%m的余数为j的最大值。背包dp转移就可以。

代码

#include<bits/stdc++.h>
using namespace std; int n,m,k;
const int maxn = 3*100000+7;
long long dp[maxn][11];
int a[maxn];
int main(){
long long ans = 0;
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
dp[i][j]=-1e9;
}
}
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
dp[i][1%m]=a[i]-k;
ans=max(ans,dp[i][1%m]);
}
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
int pre = (j-1+m)%m;
if (pre == 0) {
dp[i][j]=max(dp[i][j],dp[i-1][pre]+a[i]-k);
} else {
dp[i][j]=max(dp[i][j],dp[i-1][pre]+a[i]);
}
ans=max(ans,dp[i][j]);
}
}
printf("%lld\n",ans);
}

Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp的更多相关文章

  1. Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 【数学+分块】

    一.题目 D. Yet Another Subarray Problem 二.分析 公式的推导时参考的洛谷聚聚们的推导 重点是公式的推导,推导出公式后,分块是很容易想的.但是很容易写炸. 1 有些地方 ...

  2. Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code

    Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code 题目链接 题意: 给出\(n\)个俄罗斯套娃,每个套娃都有一个\( ...

  3. Educational Codeforces Round 69 (Rated for Div. 2)

                                                                                                  A. DIY ...

  4. Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting 水题

    C. Array Splitting You are given a sorted array

  5. Educational Codeforces Round 69 (Rated for Div. 2) A~D Sloution

    A. DIY Wooden Ladder 题意:有一些不能切的木板,每个都有一个长度,要做一个梯子,求梯子的最大台阶数 做梯子的木板分为两种,两边的两条木板和中间的若干条台阶木板 台阶数为 $k$ 的 ...

  6. Educational Codeforces Round 69 (Rated for Div. 2)D(DP,思维)

    #include<bits/stdc++.h>using namespace std;int a[300007];long long sum[300007],tmp[300007],mx[ ...

  7. Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting (思维)

    题意:给你一个长度为\(n\)的升序序列,将这个序列分成\(k\)段,每一段的值为最大值和最小值的差,求\(k\)段值的最小和. 题解:其实每一段的最大值和最小值的差,其实就是这段元素的差分和,因为是 ...

  8. Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again(DP)

    链接: https://codeforces.com/contest/1221/problem/D 题意: You have a fence consisting of n vertical boar ...

  9. Educational Codeforces Round 130 (Rated for Div. 2) C. awoo's Favorite Problem

    https://codeforc.es/contest/1697/problem/C 因为规则中,两种字符串变换都与'b'有关,所以我们根据b的位置来进行考虑: 先去掉所有的'b',如果两字符串不相等 ...

随机推荐

  1. python-11-字典的增删改查

    前言 1.dict 字典:{key,vlaue} --key 必须是不可变数据类型,可哈希,--value:任意数据类型 2.dict优点:二分查找去查询--存储大量的关系型数据,可哈希.--无序的, ...

  2. url中拼接中文参数,后台接收为乱码的问题

    遇到在URL中拼接中文的参数,后台拿到的数据为乱码的问题,这里来说一下问题出现的原因与解决方法. 大家比较关心的应该是解决的方法,因此先说解决方法. 解决方法 解决的方法是在客户端对这个中文参数进行编 ...

  3. C#A类派生类强转基类IL居然还是可以调用派生类中方法的例子

    大家都知道在C#中,如果B类继承自A类,如果一个对象是B类型的但是转换为A类型之后,这个对象是无法在调用属于B类型的方法的,如下例子: 基类A: public class A { } 派生类B: pu ...

  4. MySQL学习——操作表

    MySQL学习——操作表 摘要:本文主要学习了使用DDL语句操作表的方法. 创建表 语法 create table 表名 [表定义选项] [表选项]; 表定义选项 用来创建定义表的结构,由列名(col ...

  5. SpringCloud之API网关与服务发现——Cloud核心组件实战入门及原理

    微服务发展历史 单体模式——>服务治理(服务拆分)——>微服务(细分服务)——>Segments(服务网格) 微服务 VS SOA 微服务:模块化.独立部署.异构化 SOA:共同的治 ...

  6. Spring高频面试题,你能答的上哪些?(高级篇)

    1.什么是 Spring 框架?Spring 框架有哪些主要模块? 2.使用 Spring 框架能带来哪些好处? 3.什么是控制反转(IOC)?什么是依赖注入? 4.请解释下 Spring 框架中的 ...

  7. FCC---Create Visual Direction by Fading an Element from Left to Right---一个带好看背景色的圆形图案,从左到右移动,透明度opacity渐变为0.1,背景色渐渐消失的效果

    For this challenge, you'll change the opacity of an animated element so it gradually fades as it rea ...

  8. 初始HTML_二

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

  9. MySQL基于报错注入2

    目标站点: 0x1 注入点判断 http://www.xxxxxx.com/pages/services.php?id=1 #true http://www.xxxxxx.com/pages/serv ...

  10. iOS 禁用`URL Scheme`和`Universal Link`(通用链接)

    为什么要禁用URL Scheme和Universal Link(通用链接) 通常我们APP中都会嵌套一些web页面,有时我们的web页面会被DNS劫持从而跳转到其他APP中:或者是某些APP的Univ ...