#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
int pf[100001];
map<tuple<int,int,int>,int>mem;
int cost(int s,int e){
return (pf[e]-pf[s])*(e-s);
}
int solve(int s,int e,int k){
if(k==0){
return cost(s,e);
}
if(mem.find({s,e,k})!=mem.end()){
return mem[{s,e,k}];
}
int k1=k-1,k2=0;
auto calc=[&](int i)->int{
return solve(s,i,k1)+solve(i,e,k2);
};
int l=s+k1+1,h=e-k2-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;
}
}
int res=INF;
for(int i=l;i<=h;i++){
res=min(res,calc(i));
}
mem[{s,e,k}]=res;
return res;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N,K;
cin>>N>>K;
vector<int>v(N);
for(int i=0;i<N;i++){
cin>>v[i];
}
for(int i=0;i<N;i++){
pf[i+1]=pf[i]+v[i];
}
cout<<solve(0,N,K)<<'\n';
// cout<<K<<" "<<mem.size()<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1840 KiB | ||||
2 | Accepted | 578ms | 10428 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2320 KiB | ||||
4 | Accepted | 3ms | 2308 KiB | ||||
5 | Accepted | 3ms | 2520 KiB | ||||
6 | Accepted | 3ms | 2864 KiB | ||||
7 | Accepted | 3ms | 2836 KiB | ||||
subtask3 | 0/10 | ||||||
8 | Accepted | 4ms | 3148 KiB | ||||
9 | Accepted | 14ms | 3608 KiB | ||||
10 | Wrong answer | 32ms | 4272 KiB | ||||
11 | Accepted | 74ms | 5300 KiB | ||||
12 | Accepted | 186ms | 7948 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Accepted | 4ms | 3972 KiB | ||||
14 | Accepted | 27ms | 4752 KiB | ||||
15 | Accepted | 193ms | 7460 KiB | ||||
16 | Accepted | 1.534s | 22312 KiB | ||||
17 | Time limit exceeded | 3.059s | 20324 KiB | ||||
18 | Time limit exceeded | 3.019s | 24744 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Accepted | 230ms | 10968 KiB | ||||
20 | Time limit exceeded | 3.059s | 18792 KiB | ||||
21 | Time limit exceeded | 3.068s | 20256 KiB | ||||
22 | Time limit exceeded | 3.101s | 21608 KiB | ||||
23 | Time limit exceeded | 3.068s | 23148 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Accepted | 14ms | 7320 KiB | ||||
25 | Accepted | 14ms | 7316 KiB | ||||
26 | Accepted | 14ms | 7628 KiB | ||||
27 | Accepted | 17ms | 8136 KiB | ||||
28 | Wrong answer | 56ms | 10976 KiB | ||||
29 | Wrong answer | 425ms | 17648 KiB | ||||
30 | Time limit exceeded | 3.072s | 20284 KiB | ||||
31 | Time limit exceeded | 3.059s | 20876 KiB | ||||
32 | Time limit exceeded | 3.058s | 21724 KiB | ||||
33 | Time limit exceeded | 3.059s | 23076 KiB | ||||
34 | Time limit exceeded | 3.078s | 24712 KiB | ||||
35 | Time limit exceeded | 3.075s | 27768 KiB | ||||
36 | Time limit exceeded | 3.072s | 32556 KiB | ||||
37 | Time limit exceeded | 3.065s | 38920 KiB | ||||
38 | Time limit exceeded | 3.038s | 61176 KiB | ||||
39 | Time limit exceeded | 3.068s | 71492 KiB | ||||
40 | Time limit exceeded | 3.066s | 88768 KiB |