#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=1;i<=N;i++){
cin>>v[i];
pf[i]=pf[i-1]+v[i];
}
vector<vector<int>>dp(K+1,vector<int>(N+1,INF));
vector<vector<int>>p(K+1,vector<int>(N+1));
dp[0][0]=0;
for(int i=1;i<=N;i++){
dp[0][i]=pf[i]*i;
}
for(int i=1;i<=K;i++){
for(int j=i;j<=N;j++){
auto calc=[&](int k)->int{
return dp[i-1][k]+(pf[j]-pf[k])*(j-k);
};
int l=0,h=j-1;
while(h-l>=3){
int m1=l+(h-l)/3;
int m2=h-(h-l)/3;
if(calc(m1)<=calc(m2)){
h=m2;
}
else{
l=m1;
}
}
for(int k=l;k<=h;k++){
int c=calc(k);
if(c<dp[i][j]){
dp[i][j]=c;
p[i][j]=k;
}
}
}
}
cout<<dp[K][N]<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1828 KiB | ||||
2 | Accepted | 20ms | 5444 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2340 KiB | ||||
4 | Accepted | 3ms | 2632 KiB | ||||
5 | Accepted | 3ms | 2716 KiB | ||||
6 | Accepted | 3ms | 2728 KiB | ||||
7 | Accepted | 3ms | 2708 KiB | ||||
subtask3 | 0/10 | ||||||
8 | Accepted | 3ms | 3024 KiB | ||||
9 | Accepted | 3ms | 3304 KiB | ||||
10 | Wrong answer | 4ms | 3440 KiB | ||||
11 | Accepted | 4ms | 3960 KiB | ||||
12 | Accepted | 8ms | 4700 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Accepted | 6ms | 4556 KiB | ||||
14 | Wrong answer | 8ms | 4888 KiB | ||||
15 | Accepted | 12ms | 5912 KiB | ||||
16 | Accepted | 39ms | 10672 KiB | ||||
17 | Accepted | 94ms | 20212 KiB | ||||
18 | Accepted | 449ms | 82820 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Wrong answer | 71ms | 16484 KiB | ||||
20 | Accepted | 192ms | 32676 KiB | ||||
21 | Wrong answer | 620ms | 87700 KiB | ||||
22 | Wrong answer | 1.235s | 166404 KiB | ||||
23 | Wrong answer | 2.434s | 323620 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Accepted | 17ms | 15092 KiB | ||||
25 | Accepted | 43ms | 18844 KiB | ||||
26 | Accepted | 70ms | 22616 KiB | ||||
27 | Accepted | 94ms | 26532 KiB | ||||
28 | Wrong answer | 120ms | 30308 KiB | ||||
29 | Accepted | 149ms | 34428 KiB | ||||
30 | Wrong answer | 277ms | 50680 KiB | ||||
31 | Wrong answer | 671ms | 98280 KiB | ||||
32 | Accepted | 1.332s | 177396 KiB | ||||
33 | Wrong answer | 2.66s | 334804 KiB | ||||
34 | Runtime error | 206ms | 521632 KiB | ||||
35 | Runtime error | 196ms | 521608 KiB | ||||
36 | Runtime error | 196ms | 521580 KiB | ||||
37 | Runtime error | 197ms | 521552 KiB | ||||
38 | Runtime error | 197ms | 521548 KiB | ||||
39 | Runtime error | 196ms | 521544 KiB | ||||
40 | Runtime error | 196ms | 521528 KiB |