#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);
};
auto solve=[&](int y)->pair<int,int>{
vector<int>dp(N+1,1e18),cnt(N+1);
deque<int>q;
int o=0;
auto bs=[&](int l,int i)->int{
int h=N;
int ll=l;
while(l<h){
int m=(l+h)/2;
if(dp[i]+cost(i,m)<=dp[o]+cost(o,m)){
h=m;
}
else{
l=m+1;
}
}
return h;
};
dp[0]=-y;
cnt[0]=-1;
for(int i=1;i<=N;i++){
while(!q.empty()){
int j=q.front();
if(dp[j]+cost(j,i)<dp[o]+cost(o,i)){
o=j;
q.pop_front();
}
else if(j<o){
q.pop_front();
}
else{
break;
}
}
dp[i]=dp[o]+cost(o,i)+y;
cnt[i]=cnt[o]+1;
while(!q.empty()&&bs(i,i)<=bs(i,q.back())){
q.pop_back();
}
q.push_back(i);
}
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 | 1824 KiB | ||||
2 | Wrong answer | 37ms | 2460 KiB | ||||
subtask2 | 0/10 | ||||||
3 | Wrong answer | 3ms | 2284 KiB | ||||
4 | Wrong answer | 3ms | 2372 KiB | ||||
5 | Wrong answer | 3ms | 2500 KiB | ||||
6 | Wrong answer | 3ms | 2720 KiB | ||||
7 | Wrong answer | 3ms | 2828 KiB | ||||
subtask3 | 0/10 | ||||||
8 | Wrong answer | 6ms | 3196 KiB | ||||
9 | Wrong answer | 4ms | 3404 KiB | ||||
10 | Wrong answer | 4ms | 3488 KiB | ||||
11 | Wrong answer | 4ms | 3444 KiB | ||||
12 | Wrong answer | 4ms | 3532 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Wrong answer | 45ms | 3732 KiB | ||||
14 | Wrong answer | 43ms | 3684 KiB | ||||
15 | Wrong answer | 43ms | 4008 KiB | ||||
16 | Wrong answer | 41ms | 3976 KiB | ||||
17 | Wrong answer | 37ms | 4012 KiB | ||||
18 | Wrong answer | 37ms | 4320 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Wrong answer | 432ms | 7396 KiB | ||||
20 | Wrong answer | 430ms | 7780 KiB | ||||
21 | Wrong answer | 437ms | 8360 KiB | ||||
22 | Wrong answer | 426ms | 8492 KiB | ||||
23 | Wrong answer | 412ms | 8620 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Wrong answer | 871ms | 11740 KiB | ||||
25 | Wrong answer | 870ms | 11864 KiB | ||||
26 | Wrong answer | 871ms | 11696 KiB | ||||
27 | Wrong answer | 870ms | 11696 KiB | ||||
28 | Wrong answer | 871ms | 12468 KiB | ||||
29 | Wrong answer | 866ms | 12596 KiB | ||||
30 | Wrong answer | 871ms | 12552 KiB | ||||
31 | Wrong answer | 870ms | 12560 KiB | ||||
32 | Wrong answer | 870ms | 12712 KiB | ||||
33 | Wrong answer | 880ms | 12636 KiB | ||||
34 | Wrong answer | 870ms | 12760 KiB | ||||
35 | Wrong answer | 834ms | 12680 KiB | ||||
36 | Wrong answer | 799ms | 12620 KiB | ||||
37 | Wrong answer | 808ms | 12644 KiB | ||||
38 | Wrong answer | 799ms | 12604 KiB | ||||
39 | Wrong answer | 755ms | 12612 KiB | ||||
40 | Wrong answer | 538ms | 12616 KiB |