#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+2);
int o=0;
auto bs=[&](int l,int i)->int{
int 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(dp[j]+cost(j,i)<dp[o]+cost(o,i)){
o=j;
}
if(j>=o){
chk[bs(i+1,j)].push_back(j);
}
}
dp[i]=dp[o]+cost(o,i)+y;
cnt[i]=cnt[o]+1;
chk[bs(i+1,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 | Runtime error | 3ms | 1936 KiB | ||||
2 | Accepted | 163ms | 3548 KiB | ||||
subtask2 | 0/10 | ||||||
3 | Runtime error | 3ms | 2212 KiB | ||||
4 | Accepted | 3ms | 2364 KiB | ||||
5 | Accepted | 3ms | 2576 KiB | ||||
6 | Accepted | 3ms | 2660 KiB | ||||
7 | Runtime error | 3ms | 2512 KiB | ||||
subtask3 | 0/10 | ||||||
8 | Accepted | 13ms | 2860 KiB | ||||
9 | Accepted | 14ms | 3072 KiB | ||||
10 | Accepted | 13ms | 3024 KiB | ||||
11 | Runtime error | 4ms | 3392 KiB | ||||
12 | Runtime error | 4ms | 3596 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Accepted | 153ms | 4832 KiB | ||||
14 | Accepted | 178ms | 5260 KiB | ||||
15 | Accepted | 188ms | 5276 KiB | ||||
16 | Accepted | 179ms | 5616 KiB | ||||
17 | Accepted | 159ms | 5476 KiB | ||||
18 | Runtime error | 41ms | 5648 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Wrong answer | 2.565s | 21736 KiB | ||||
20 | Wrong answer | 2.569s | 21848 KiB | ||||
21 | Accepted | 2.559s | 21696 KiB | ||||
22 | Accepted | 2.397s | 22100 KiB | ||||
23 | Accepted | 2.223s | 22036 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Time limit exceeded | 3.062s | 21788 KiB | ||||
25 | Time limit exceeded | 3.068s | 21888 KiB | ||||
26 | Time limit exceeded | 3.062s | 21652 KiB | ||||
27 | Time limit exceeded | 3.065s | 21728 KiB | ||||
28 | Time limit exceeded | 3.075s | 21860 KiB | ||||
29 | Time limit exceeded | 3.101s | 21976 KiB | ||||
30 | Time limit exceeded | 3.073s | 21848 KiB | ||||
31 | Time limit exceeded | 3.075s | 21780 KiB | ||||
32 | Time limit exceeded | 3.071s | 21976 KiB | ||||
33 | Time limit exceeded | 3.066s | 21652 KiB | ||||
34 | Time limit exceeded | 3.066s | 21548 KiB | ||||
35 | Time limit exceeded | 3.049s | 21480 KiB | ||||
36 | Time limit exceeded | 3.033s | 21548 KiB | ||||
37 | Time limit exceeded | 3.053s | 21588 KiB | ||||
38 | Accepted | 2.96s | 40396 KiB | ||||
39 | Runtime error | 1.315s | 40264 KiB | ||||
40 | Runtime error | 1.187s | 40264 KiB |