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. 新工具解决消息丢失的bug

    最近在调查一个消息丢失的bug,所幸客户的文本文件里有丢失的记录,但在localdb文件里找不到. 我当时的想法是,在运行report的时候把丢失的记录从文本文件找出来,然后添加到localdb里,最 ...

  2. 【Oracle命令】sql语句之排序(order by)

    通过对数据库数据进行降序排序来达到显示最新数据在前面的效果 -- 降序排序(最新的显示在前面) SELECT * FROM 表名 t ORDER BY t.uploadDatetime DESC; 格 ...

  3. PHP 源码学习 | 变量类型数据结构

    前段时间因为项目需要,研究了一下在 Windows 系统下进行 PHP 扩展的开发,对于 PHP 扩展的开发并不是一件容易的事情(话又说回来了,会者不难,难者不会,关键是自己不会).我当时的需求,主要 ...

  4. pandas 学习 第7篇:DataFrame - 数据处理(应用、操作索引、重命名、合并)

    DataFrame的这些操作和Series很相似,这里简单介绍一下. 一,应用和应用映射 apply()函数对每个轴应用一个函数,applymap()函数对每个元素应用一个函数: DataFrame. ...

  5. MySQL(9)---纪录一次实际开发过程中用到的复杂存储过程

    Mysql(9)---纪录一次实际开发过程中用到的复杂存储过程 为了尽可能的还原当时为什么需要用到存储过程,下面我写了个详细的文档,我们可以从需求文档出发来分析. 有关存储过程之前也写了两篇文章来做铺 ...

  6. python基础(30):黏包、socket的其他方法

    1. 黏包 1.1 黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) 同时执行多条命令之后,得到的结果很可能只有一部分,在执行其他命令的时候又接 ...

  7. java基础(11):接口、多态

    1. 接口 1.1 接口概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来完成. ...

  8. Java8 日期和时间API

    LocalDate.LocalTime.Instant.Duration.Period 1.1使用LocalDate和LocalTime 1.1.1LocalDate的创建方式和相关方法使用示例 @T ...

  9. ThreadLocal(线程本地存储)

    1. ThreadLocal,即线程本地变量或线程本地存储. threadlocal的作用是提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或组件之间一些公共变量传递的 ...

  10. Qt Creator配置MSVC调试器

    安装完QT后会看到Qt Creator中的MSVC构建组件带有黄色的感叹号,那是因为没有调试器的原因.由于Qt-MSVC版本套件没有默认安装调试器, 需要我们自己手动下载安装. 根据官方文档http: ...