xtu 1264 Partial Sum 2017湘潭邀请赛E

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

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <queue>
#include <cmath>
using namespace std;
 
#define ll __int64
const int N=100005;
 
int main()
{
    ll n,m,c,a,sum[N];
    while(~scanf("%I64d %I64d %I64d",&n,&m,&c))
    {
        sum[0]=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%I64d",&a);
            sum[i]=sum[i-1]+a;
        }
        sort(sum,sum+n+1);
        int l=n,s=0;
        ll ans=0;
        for(int i=0; i<m; i++)
        {
            ll t=abs(sum[n-i]-sum[i])-c;
            if(t<=0) break;
            ans+=t;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/A852852852852/article/details/72236661
个人分类: c/c++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭