#include<bits/stdc++.h>
using namespace std;
int p[50000];
int k;
long long cut(int cuttime,int left,int right){
if(cuttime>=k) return 0;
if(right-left+1<3) return 0;
long long sub = 0;
for(int i=left;i<=right;i++) sub+=p[i];
long long x[right-left-1];
for(int i=0;i<right-left-1;i++) x[i] = 0;
x[0] -= p[left];
for(int i=left+2;i<=right;i++) x[0] += (i-left-1)*p[i];
for(int i=1;i<right-left-1;i++){
x[i] = x[i-1]-sub;
}
x[0] = abs(x[0]);
long long min = x[0];
int cutid = left+1;
for(int i=0;i<right-left-1;i++){
if(x[i]<0) x[i]*=-1;
if(x[i]<min){
min = x[i];
cutid = left+1+i;
}
}
cuttime++;
return p[cutid]+cut(cuttime,left,cutid-1)+cut(cuttime,cutid+1,right);
}
int main(){
int n;
cin>>n>>k;
for(int i=0;i<n;i++) cin>>p[i];
cout<<cut(0,0,n-1);
}
#include<bits/stdc++.h>
using namespace std;
int p[50000];
int k;
long long cut(int cuttime,int left,int right){
if(cuttime>=k) return 0;
if(right-left+1<3) return 0;
long long sub = 0;
for(int i=left;i<=right;i++) sub+=p[i];
long long x[right-left-1];
for(int i=0;i<right-left-1;i++) x[i] = 0;
x[0] -= p[left];
for(int i=left+2;i<=right;i++) x[0] += (i-left-1)*p[i];
for(int i=1;i<right-left-1;i++){
x[i] = x[i-1]-sub;
}
x[0] = abs(x[0]);
long long min = x[0];
int cutid = left+1;
for(int i=0;i<right-left-1;i++){
if(x[i]<0) x[i]*=-1;
if(x[i]<min){
min = x[i];
cutid = left+1+i;
}
}
cuttime++;
return p[cutid]+cut(cuttime,left,cutid-1)+cut(cuttime,cutid+1,right);
}
int main(){
int n;
cin>>n>>k;
for(int i=0;i<n;i++) cin>>p[i];
cout<<cut(0,0,n-1);
}
喔沒事了,沒加long long