CF1175D Array Splitting
The meaning of the question: Give you a set of numbers and let you divide it into k-end segments. Each number in each segment is multiplied by the segment number.
Finally, what is the minimum value after such segmentation
problem analysis:
The b array is the prefix and the array, and r is the right coordinate of each group (for easy understanding, don't delve into the +1 -1 problem, it will be contracted out in the end anyway) It
is divided into k groups in total~
sum=b[r1]*1+(b[r2]-b[r1])*2+(b[r3]-b[r2])*3.....+(b[rk]-b[r(k-1)])*k
=k*b[rk]-b[r1]-b[r2]-.....-b[r(k-1);
Because it k*b【rk】
is the last paragraph, it is equivalent to
k*b[rn] This is a fixed value, so we only need to ensure that the minimum value of the selected k-1 end is done.
Sort once and select the first k-1
because it is the selection here. It is equivalent to the position of the end of the segment, and each relative segment is also determined, so it can be sorted and the order relationship does not affect
AC code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<sstream>
#include<string.h>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
#define lowbit(x) (x)&(-x)
#define mem(a,b) memset((a),(b),sizeof(a));
const ll inf=0x3f3f3f3f;//1061109567,2*未超int,allinf=mem(a,0x3f,sizeof(a));
typedef pair<int ,int > PII;
const int nn=1e6+10;
ll a[nn],b[nn];
int main()
{
//#define io
#ifdef io
freopen("in.txt","r",stdin);
#endif
ll n,m,ans;
cin>>n>>m;
ans=0;
mem(a,0);
mem(b,0);
for(int i=1;i<=n;i++)cin>>a[i],b[i]+=b[i-1]+a[i];
sort(b+1,b+n);
ans=m*b[n];
for(int i=1;i<m;i++)ans-=b[i];
cout<<ans<<endl;
return 0;
}