// Lagrangian Relaxation & Li Chao Tree
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
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 solve=[&](int y)->pair<int,int>{
vector<int>dp(N+1,INF),cnt(N+1),t((N+1)*4);
auto cost=[&](int s,int e)->int{
return dp[s]+(pf[e]-pf[s])*(e-s);
};
auto add=[&](int i,int l,int r,int p)->void{
while(true){
int m=(l+r)/2;
bool ll=cost(p,l)<cost(t[i],l);
bool mm=cost(p,m)<cost(t[i],m);
if(mm){
swap(t[i],p);
}
if(l==r){
break;
}
if(ll!=mm){
i=i*2+1;
r=m;
}
else{
i=i*2+2;
l=m+1;
}
}
};
auto get=[&](int i,int l,int r,int x)->int{
int bc=INF,bi=0;
while(true){
int m=(l+r)/2;
int c=cost(t[i],x);
if(c<bc){
bc=c;
bi=t[i];
}
if(l==r){
break;
}
if(x<m){
i=i*2+1;
r=m;
}
else{
i=i*2+2;
l=m+1;
}
}
return bi;
};
dp[0]=-y;
cnt[0]=-1;
add(0,0,N,0);
for(int i=1;i<=N;i++){
int o=get(0,0,N,i);
dp[i]=cost(o,i)+y;
cnt[i]=cnt[o]+1;
add(0,0,N,i);
}
return{dp[N],cnt[N]};
};
int l=0,h=INF;
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 | Accepted | 61ms | 2680 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2272 KiB | ||||
4 | Accepted | 3ms | 2356 KiB | ||||
5 | Accepted | 3ms | 2484 KiB | ||||
6 | Accepted | 3ms | 2580 KiB | ||||
7 | Accepted | 3ms | 2776 KiB | ||||
subtask3 | 10/10 | ||||||
8 | Accepted | 7ms | 3160 KiB | ||||
9 | Accepted | 7ms | 3472 KiB | ||||
10 | Accepted | 7ms | 3424 KiB | ||||
11 | Accepted | 8ms | 3556 KiB | ||||
12 | Accepted | 8ms | 3768 KiB | ||||
subtask4 | 20/20 | ||||||
13 | Accepted | 63ms | 4264 KiB | ||||
14 | Accepted | 65ms | 4440 KiB | ||||
15 | Accepted | 68ms | 4284 KiB | ||||
16 | Accepted | 71ms | 4500 KiB | ||||
17 | Accepted | 71ms | 4644 KiB | ||||
18 | Accepted | 71ms | 4720 KiB | ||||
subtask5 | 29/29 | ||||||
19 | Accepted | 703ms | 10316 KiB | ||||
20 | Accepted | 728ms | 10216 KiB | ||||
21 | Accepted | 749ms | 10096 KiB | ||||
22 | Accepted | 759ms | 10300 KiB | ||||
23 | Accepted | 764ms | 10300 KiB | ||||
subtask6 | 31/31 | ||||||
24 | Accepted | 1.22s | 16600 KiB | ||||
25 | Accepted | 1.32s | 16616 KiB | ||||
26 | Accepted | 1.401s | 16608 KiB | ||||
27 | Accepted | 1.434s | 16556 KiB | ||||
28 | Accepted | 1.424s | 16672 KiB | ||||
29 | Accepted | 1.475s | 16552 KiB | ||||
30 | Accepted | 1.5s | 16524 KiB | ||||
31 | Accepted | 1.531s | 16560 KiB | ||||
32 | Accepted | 1.557s | 16556 KiB | ||||
33 | Accepted | 1.565s | 16560 KiB | ||||
34 | Accepted | 1.592s | 16676 KiB | ||||
35 | Accepted | 1.595s | 16668 KiB | ||||
36 | Accepted | 1.597s | 16672 KiB | ||||
37 | Accepted | 1.603s | 16676 KiB | ||||
38 | Accepted | 1.588s | 16684 KiB | ||||
39 | Accepted | 1.572s | 16644 KiB | ||||
40 | Accepted | 1.518s | 16676 KiB |