You are given an array a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,…,an and two integers ? and ?.
You can choose some subarray a l , a l + 1 , … , a r − 1 , a r a_l,a_{l+1},…,a_{r-1},a_r al,al+1,…,ar−1,ar.
The cost of subarray a l , a l + 1 , … , a r − 1 , a r a_l,a_{l+1},…,a_{r-1},a_r al,al+1,…,ar−1,ar is equal to ∑ i = l r a i − k × ⌈ r − l + 1 m ⌉ \sum_{i=l}^{r}{a_i}-k\times \lceil \frac{r-l+1}{m}\rceil ∑i=lrai−k×⌈mr−l+1⌉,where ⌈ x ⌉ \lceil x \rceil ⌈x⌉ is the least integer greater than or equal to x x x.
The cost of empty subarray is equal to zero.
For example, if m = 3 , k = 10 m=3, k=10 m=3,k=10 and a = [ 2 , − 4 , 15 , − 3 , 4 , 8 , 3 ] a=[2,−4,15,−3,4,8,3] a=[2,−4,15,−3,4,8,3], then the cost of some subarrays are:
a 3 … a 3 : 15 − k ⌈ 1 3 ⌉ = 15 − 10 = 5 ; a_3…a_3:15−k\lceil \frac{1}{3} \rceil=15−10=5; a3…a3:15−k⌈31⌉=15−10=5;
a 3 … a 4 : ( 15 − 3 ) − ⌈ 1 3 ⌉ = 12 − 10 = 2 ; a_3…a_4:(15−3)−\lceil \frac{1}{3} \rceil=12−10=2; a3…a4:(15−3)−⌈31⌉=12−10=2;
a 3 … a 5 : ( 15 − 3 + 4 ) − ⌈ 1 3 ⌉ = 16 − 10 = 6 ; a_3…a_5:(15−3+4)−\lceil \frac{1}{3} \rceil=16−10=6; a3…a5:(15−3+4)−⌈31⌉=16−10=6;
a 3 … a 6 : ( 15 − 3 + 4 + 8 ) − ⌈ 1 3 ⌉ = 24 − 20 = 4 ; a_3…a_6:(15−3+4+8)−\lceil \frac{1}{3} \rceil=24−20=4; a3…a6:(15−3+4+8)−⌈31⌉=24−20=4;
a 3 … a 7 : ( 15 − 3 + 4 + 8 + 3 ) − ⌈ 1 3 ⌉ = 27 − 20 = 7. a_3…a_7:(15−3+4+8+3)−\lceil \frac{1}{3} \rceil=27−20=7. a3…a7:(15−3+4+8+3)−⌈31⌉=27−20=7.
Your task is to find the maximum cost of some subarray (possibly empty) of array a a a.
The first line contains three integers n , m , a n d k ( 1 ≤ n ≤ 3 × 1 0 5 , 1 ≤ m ≤ 10 , 1 ≤ k ≤ 1 0 9 ) n, m, and\ k (1\leq n \leq 3\times 10^5,1\leq m \leq 10,1\leq k\leq 10^9) n,m,and k(1≤n≤3×105,1≤m≤10,1≤k≤109).
The second line contains n n n integers a 1 , a 2 , … , a n ( − 1 0 9 ≤ a i ≤ 1 0 9 ) a_1,a_2,…,a_n (−10^9\leq a_i \leq10^9) a1,a2,…,an(−109≤ai≤109).
Print the maximum cost of some subarray of array a a a.
7 3 10
2 -4 15 -3 4 8 3
7
5 2 1000
-13 -4 -9 -20 -11
output
0
#include
using namespace std;
const int maxn=3e5+10;
int n,m;
long long a[maxn],k;
long long cur[100],sum[maxn];
int solve(int b)
{
return b%m==0?b/m:b/m+1;
}
long long dfs(int l,int r)
{
if(l==r) return a[l]-k;
int mid=(l+r)>>1;long long res=-1e18;
for(int i=0;i<m;i++) cur[i]=-1e18;sum[mid+1]=0;
for(int i=mid;i>=l;i--) {
sum[i]=a[i]+sum[i+1];
cur[(mid-i+1)%m]=max(cur[(mid-i+1)%m],sum[i]-k*solve(mid-i+1));
}
sum[mid]=0;
for(int i=mid+1;i<=r;i++) {
int now=(i-mid)%m;sum[i]=sum[i-1]+a[i];
for(int j=0;j<m;j++){
if(j==0) res=max(res,cur[j]+sum[i]-k*solve(i-mid));
else {
if(j+now>m) res=max(res,cur[j]+sum[i]-k*solve(i-mid));
else res=max(res,cur[j]+sum[i]-k*((i-mid)/m));
}
}
}
return max(res,max(dfs(l,mid),dfs(mid+1,r)));
}
int main()
{
scanf("%d %d %lld",&n,&m,&k);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
printf("%lld\n",max(0LL,dfs(1,n)));
}
#include
using namespace std;
const int maxn=3e5+10;
int n,m,k,a[maxn];
long long dp[maxn][12],sum[maxn];
int main()
{
scanf("%d %d %d",&n,&m,&k);long long ans=0;
for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i];
for(int i=1;i<=n;i++) {
if(i-m>=0) dp[i][0]=max(dp[i][0],dp[i-m][0]+sum[i]-sum[i-m]-k);
for(int j=1;j<m;j++) {
if(i-j>=0) dp[i][j]=max(dp[i][j],sum[i]-sum[i-j]-k);
if(i>=m) dp[i][j]=max(dp[i][j],sum[i]-sum[i-m]-k+dp[i-m][j]);
}
for(int j=0;j<m;j++) ans=max(ans,dp[i][j]);
}
printf("%lld\n",ans);
}