codeforces 1197D Yet Another Subarray Problem dp

http://codeforces.com/problemset/problem/1197/D

You are given an array a1,a2,…,an and two integers m and k

.

You can choose some subarray al,al+1,…,ar−1,ar

.

The cost of subarray al,al+1,…,ar−1,ar

is equal to ∑i=lrai−k⌈r−l+1m⌉, where ⌈x⌉ 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:

  • a3…a3:15−k⌈13⌉=15−10=5
  • ;
  • a3…a4:(15−3)−k⌈23⌉=12−10=2
  • ;
  • a3…a5:(15−3+4)−k⌈33⌉=16−10=6
  • ;
  • a3…a6:(15−3+4+8)−k⌈43⌉=24−20=4
  • ;
  • a3…a7:(15−3+4+8+3)−k⌈53⌉=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≤n≤3⋅105,1≤m≤10,1≤k≤109

).

The second line contains n

integers a1,a2,…,an (−109≤ai≤109

).

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

思路:因为题目要求是连续的区间,在考虑从位置j开始的情况时,只要区间[i,j-1]对答案的贡献是正的,那么就可以把这段区间加上。用dp[i]表示以i为右端点向前任意m的倍数的范围内内所能取得的最优解,那么当i>=m时,有dp[i]=max(0,dp[i-m]+(a[i-m+1]+……+a[i])-k)。其中对数组a的求和可以用前缀和数组来优化。对于[i+1,i+m-1]内的情况计算一遍并更新即可。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;

int n,m,k;
ll a[300005];
ll sum[300005];
ll dp[300005];


int main()
{
    scanf("%d %d %d",&n,&m,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        sum[i]=sum[i-1]+a[i];
    }
    ll ans=0;
    for(int i=0;i<=n;i++)
    {
        if(i>=m)
            dp[i]=max((ll)0,dp[i-m]+sum[i]-sum[i-m]-k);
        ans=max(ans,dp[i]);
        for(int j=1;j<m;j++)
        {
            if(i+j<=n)
                ans=max(ans,dp[i]+sum[i+j]-sum[i]-k);
        }
    }
    printf("%lld\n",ans);
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值