Partial Sum (2017湘潭)

Partial Sum

   

Partial Sum

Bobo has a integer sequence  a1,a2,,an  of length  n . Each time, he selects two ends  0l<rn  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.

Input

The 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 .

  • 2n105
  • 12mn+1
  • |ai|,C104
  • The sum of  n  does not exceed  106 .

Output

For each test cases, output an integer which denotes the maximum.

Sample Input

4 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 Output

3
4
2
0

Source

XTU OnlineJudge

题意:输入n个数字你可已选择(0~m)对个端点,不过每个端点只能选择一次;然后选择的端点带入表达式 |rj=l+1aj|C  ,题意要求选择端点得最大值;
思路:根据目标表达式,我们知道要求的是两个端点之间的a[]值的和,所以预处理一下前缀和S[],则S[i]-S[j-1]就是j-i之间的和;预处理完之后我们再对S[]进行排序便可;注意S[0]=0,思考一下;
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005;
const int inf=1e9;
typedef long long ll;

ll S[maxn];
int main()
{
    int n,m,c;
    while(~scanf("%d%d%d",&n,&m,&c))
    {
        memset(S,0,sizeof(S));
        for(int i=1;i<=n;i++)
        {
            int a;
            scanf("%d",&a);
            S[i]=S[i-1]+a;
        }
        sort(S,S+n+1);

//        for(int i=0;i<=n;i++)
//            printf("%d ",S[i]);
//        printf("\n");

        ll ans=0,sum=0;
        for(int i=0;i<m;i++)
        {
            sum=S[n-i]-S[i]-c;
            if(sum<=0) break;
            ans+=sum;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值