373人阅读 评论(0)

# Partial Sum

Bobo has a integer sequence  of length . Each time, he selects two ends  and add  into a counter which is zero initially. He repeats the selection for at most  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 . The second line contains  integers .

• The sum of  does not exceed .

## 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;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：57675次
• 积分：2168
• 等级：
• 排名：第19690名
• 原创：160篇
• 转载：30篇
• 译文：0篇
• 评论：1条
文章分类
最新评论