#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-2,k2=1;
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 | Runtime error | 180ms | 524876 KiB | ||||
2 | Runtime error | 219ms | 524636 KiB | ||||
subtask2 | 0/10 | ||||||
3 | Runtime error | 180ms | 524404 KiB | ||||
4 | Runtime error | 180ms | 524180 KiB | ||||
5 | Runtime error | 177ms | 523948 KiB | ||||
6 | Runtime error | 186ms | 523720 KiB | ||||
7 | Runtime error | 180ms | 523480 KiB | ||||
subtask3 | 0/10 | ||||||
8 | Runtime error | 177ms | 523372 KiB | ||||
9 | Runtime error | 225ms | 523140 KiB | ||||
10 | Runtime error | 180ms | 523136 KiB | ||||
11 | Runtime error | 221ms | 522904 KiB | ||||
12 | Runtime error | 219ms | 522676 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Runtime error | 224ms | 522456 KiB | ||||
14 | Runtime error | 180ms | 522452 KiB | ||||
15 | Runtime error | 225ms | 522464 KiB | ||||
16 | Runtime error | 224ms | 522228 KiB | ||||
17 | Runtime error | 224ms | 522232 KiB | ||||
18 | Runtime error | 224ms | 522244 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Runtime error | 226ms | 522228 KiB | ||||
20 | Runtime error | 180ms | 522228 KiB | ||||
21 | Runtime error | 180ms | 522036 KiB | ||||
22 | Runtime error | 180ms | 522040 KiB | ||||
23 | Runtime error | 228ms | 521808 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Accepted | 14ms | 7900 KiB | ||||
25 | Runtime error | 234ms | 521808 KiB | ||||
26 | Runtime error | 231ms | 521816 KiB | ||||
27 | Runtime error | 234ms | 521792 KiB | ||||
28 | Runtime error | 234ms | 521816 KiB | ||||
29 | Runtime error | 231ms | 521824 KiB | ||||
30 | Runtime error | 231ms | 521816 KiB | ||||
31 | Runtime error | 236ms | 521588 KiB | ||||
32 | Runtime error | 234ms | 521560 KiB | ||||
33 | Runtime error | 236ms | 521564 KiB | ||||
34 | Runtime error | 238ms | 521584 KiB | ||||
35 | Runtime error | 233ms | 521576 KiB | ||||
36 | Runtime error | 234ms | 521588 KiB | ||||
37 | Runtime error | 190ms | 521396 KiB | ||||
38 | Runtime error | 189ms | 521396 KiB | ||||
39 | Runtime error | 186ms | 521392 KiB | ||||
40 | Runtime error | 231ms | 521400 KiB |