#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N,K;
cin>>N>>K;
vector<int>v(N+1),pf(N+1);
for(int i=0;i<N;i++){
cin>>v[i];
pf[i+1]=pf[i]+v[i];
}
auto cost=[&](int s,int e)->int{
return (pf[e]-pf[s])*(e-s);
};
// if(1){
// N=15;
// v.resize(N+1),pf.resize(N+1);
// for(int i=0;i<N;i++){
// v[i]=rand()%100;
// pf[i+1]=pf[i]+v[i];
// }
// for(int i=1;i<=N;i++){
// for(int j=1;j<=N;j++){
// cout<<setw(4)<<cost(i,j)<<' ';
// }
// cout<<'\n';
// }
// return 1;
// }
auto solve=[&](int y)->pair<int,int>{
vector<int>dp(N+1,1e18),cnt(N+1);
dp[0]=-y;
cnt[0]=-1;
for(int i=1,o=0;i<=N;i++){
for(int j=o;j<i;j++){
int x=dp[j]+cost(j,i)+y;
if(x<dp[i]){
dp[i]=x;
cnt[i]=cnt[j]+1;
o=j;
}
}
}
return{dp[N],cnt[N]};
};
int l=0,h=1e12;
while(l<h){
int m=(l+h)/2;
auto [x,c]=solve(m);
if(c<=K){
h=m;
}
else{
l=m+1;
}
}
auto [x,c]=solve(h);
cout<<x-c*h<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1832 KiB | ||||
2 | Accepted | 68ms | 2556 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2404 KiB | ||||
4 | Accepted | 3ms | 2396 KiB | ||||
5 | Accepted | 3ms | 2628 KiB | ||||
6 | Accepted | 3ms | 2716 KiB | ||||
7 | Accepted | 3ms | 2932 KiB | ||||
subtask3 | 10/10 | ||||||
8 | Accepted | 6ms | 3044 KiB | ||||
9 | Accepted | 4ms | 3300 KiB | ||||
10 | Accepted | 4ms | 3636 KiB | ||||
11 | Accepted | 4ms | 3740 KiB | ||||
12 | Accepted | 4ms | 3812 KiB | ||||
subtask4 | 20/20 | ||||||
13 | Accepted | 305ms | 4400 KiB | ||||
14 | Accepted | 222ms | 4648 KiB | ||||
15 | Accepted | 136ms | 4792 KiB | ||||
16 | Accepted | 59ms | 4632 KiB | ||||
17 | Accepted | 43ms | 4708 KiB | ||||
18 | Accepted | 32ms | 4740 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Time limit exceeded | 3.058s | 5804 KiB | ||||
20 | Time limit exceeded | 3.085s | 6272 KiB | ||||
21 | Accepted | 2.911s | 9028 KiB | ||||
22 | Accepted | 1.524s | 9264 KiB | ||||
23 | Accepted | 912ms | 9460 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Time limit exceeded | 3.049s | 9712 KiB | ||||
25 | Time limit exceeded | 3.069s | 10656 KiB | ||||
26 | Time limit exceeded | 3.099s | 11472 KiB | ||||
27 | Time limit exceeded | 3.065s | 12156 KiB | ||||
28 | Time limit exceeded | 3.053s | 12120 KiB | ||||
29 | Time limit exceeded | 3.082s | 12064 KiB | ||||
30 | Time limit exceeded | 3.046s | 12056 KiB | ||||
31 | Time limit exceeded | 3.058s | 12248 KiB | ||||
32 | Time limit exceeded | 3.049s | 12264 KiB | ||||
33 | Time limit exceeded | 3.065s | 12180 KiB | ||||
34 | Time limit exceeded | 3.078s | 12004 KiB | ||||
35 | Accepted | 1.595s | 16088 KiB | ||||
36 | Accepted | 1.1s | 16196 KiB | ||||
37 | Accepted | 865ms | 15916 KiB | ||||
38 | Accepted | 662ms | 16120 KiB | ||||
39 | Accepted | 632ms | 16120 KiB | ||||
40 | Accepted | 606ms | 16120 KiB |