#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);
vector<vector<int>>chk(N+1);
int o=0;
auto bs=[&](int i)->int{
int l=i+1,h=N;
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++){
for(int j:chk[i]){
if(j<o){
continue;
}
if(dp[j]+cost(j,i)<dp[o]+cost(o,i)){
o=j;
}
chk[bs(j)].push_back(j);
}
dp[i]=dp[o]+cost(o,i)+y;
cnt[i]=cnt[o]+1;
chk[bs(i)].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 | 1956 KiB | ||||
2 | Wrong answer | 164ms | 3612 KiB | ||||
subtask2 | 0/10 | ||||||
3 | Accepted | 3ms | 2316 KiB | ||||
4 | Accepted | 3ms | 2484 KiB | ||||
5 | Accepted | 3ms | 2644 KiB | ||||
6 | Wrong answer | 3ms | 2640 KiB | ||||
7 | Accepted | 3ms | 2884 KiB | ||||
subtask3 | 0/10 | ||||||
8 | Accepted | 14ms | 2948 KiB | ||||
9 | Accepted | 14ms | 3200 KiB | ||||
10 | Wrong answer | 13ms | 3416 KiB | ||||
11 | Wrong answer | 12ms | 3688 KiB | ||||
12 | Wrong answer | 9ms | 3728 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Accepted | 158ms | 4884 KiB | ||||
14 | Accepted | 179ms | 4932 KiB | ||||
15 | Wrong answer | 190ms | 5012 KiB | ||||
16 | Wrong answer | 174ms | 4972 KiB | ||||
17 | Wrong answer | 157ms | 5096 KiB | ||||
18 | Wrong answer | 119ms | 5144 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Wrong answer | 2.655s | 20416 KiB | ||||
20 | Wrong answer | 2.658s | 20312 KiB | ||||
21 | Accepted | 2.634s | 20424 KiB | ||||
22 | Wrong answer | 2.444s | 20372 KiB | ||||
23 | Wrong answer | 2.234s | 20328 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Time limit exceeded | 3.062s | 20456 KiB | ||||
25 | Time limit exceeded | 3.069s | 20472 KiB | ||||
26 | Time limit exceeded | 3.062s | 20364 KiB | ||||
27 | Time limit exceeded | 3.051s | 20456 KiB | ||||
28 | Time limit exceeded | 3.101s | 20496 KiB | ||||
29 | Time limit exceeded | 3.051s | 20492 KiB | ||||
30 | Time limit exceeded | 3.016s | 20748 KiB | ||||
31 | Time limit exceeded | 3.042s | 20744 KiB | ||||
32 | Time limit exceeded | 3.053s | 20704 KiB | ||||
33 | Time limit exceeded | 3.048s | 20368 KiB | ||||
34 | Time limit exceeded | 3.075s | 20252 KiB | ||||
35 | Time limit exceeded | 3.059s | 20372 KiB | ||||
36 | Time limit exceeded | 3.066s | 20620 KiB | ||||
37 | Time limit exceeded | 3.055s | 20652 KiB | ||||
38 | Wrong answer | 2.782s | 38360 KiB | ||||
39 | Wrong answer | 2.395s | 38364 KiB | ||||
40 | Wrong answer | 2.125s | 38576 KiB |