Educational Codeforces Round 66 D Array Splitting

传送门
You are given an array a1,a2,…,an and an integer k.

You are asked to divide this array into k non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let f(i) be the index of subarray the i-th element belongs to. Subarrays are numbered from left to right and from 1 to k.

Let the cost of division be equal to ∑i=1n(ai⋅f(i)). For example, if a=[1,−2,−3,4,−5,6,−7] and we divide it into 3 subbarays in the following way: [1,−2,−3],[4,−5],[6,−7], then the cost of division is equal to 1⋅1−2⋅1−3⋅1+4⋅2−5⋅2+6⋅3−7⋅3=−9.

Calculate the maximum cost you can obtain by dividing the array a into k non-empty consecutive subarrays.


Input

The first line contains two integers n and k (1≤k≤n≤3⋅105).

The second line contains n integers a1,a2,…,an (|ai|≤106).


Output

Print the maximum cost you can obtain by dividing the array a into k nonempty consecutive subarrays.


为什么每次做cf的题总会被爆智商……
看成后缀的和,然后贪心

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 300005;
int n,k,data[MAXN];
ll suf[MAXN];
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&data[i]);
	for(int i=n;i>=1;i--)
		suf[i]=data[i]+suf[i+1];
	sort(suf+2,suf+1+n,greater<ll>());
	ll ans = 0;
	for(int i=1;i<=k;i++)
		ans += suf[i];
	cout<<ans;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值