Partial Sum | ||
Accepted : 155 | Submit : 567 | |
Time Limit : 3000 MS | Memory Limit : 65536 KB |
Partial SumBobo has a integer sequence a1,a2,…,an of length n . Each time, he selects two ends 0≤l<r≤n and add |∑rj=l+1aj|−C into a counter which is zero initially. He repeats the selection for at most m times. If each end can be selected at most once (either as left or right), find out the maximum sum Bobo may have. InputThe input contains zero or more test cases and is terminated by end-of-file. For each test case: The first line contains three integers n , m , C . The second line contains n integers a1,a2,…,an .
OutputFor each test cases, output an integer which denotes the maximum. Sample Input4 1 1 -1 2 2 -1 4 2 1 -1 2 2 -1 4 2 2 -1 2 2 -1 4 2 10 -1 2 2 -1 Sample Output3 4 2 0 SourceXTU OnlineJudge |
http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1264
想求出前缀和 选择连个端点然后累加 相当于两个前缀和相减
问题想当与 选多对前缀和相减值再相加 和最大
先将前缀和排序 然后最大减最小 第二大减第二小...................
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll __int64
ll a[150000];
ll sum[150000];
int main()
{
int n,m;
ll C;
while(~scanf("%d%d%I64d",&n,&m,&C))
{
for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
for(int i=1;i<=n;i++)
{
if(i==1)sum[i]=a[i];
sum[i]=sum[i-1]+a[i];
}
sort(sum,sum+1+n);
ll cnt=0;
ll ans=0;
for(int i=0;i<m;i++)
{
cnt+=(sum[n-i]-sum[i]-C);
ans=max(ans,cnt);
}
printf("%I64d\n",ans);
}
return 0;
}