#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,k2;
if(k==1){
k1=0;
k2=0;
}
else{
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 | Accepted | 3ms | 1840 KiB | ||||
2 | Accepted | 913ms | 72356 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2296 KiB | ||||
4 | Accepted | 3ms | 2512 KiB | ||||
5 | Accepted | 3ms | 2568 KiB | ||||
6 | Accepted | 3ms | 2676 KiB | ||||
7 | Accepted | 3ms | 2652 KiB | ||||
subtask3 | 10/10 | ||||||
8 | Accepted | 3ms | 2960 KiB | ||||
9 | Accepted | 16ms | 4888 KiB | ||||
10 | Accepted | 43ms | 6756 KiB | ||||
11 | Accepted | 90ms | 8368 KiB | ||||
12 | Accepted | 217ms | 11552 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Accepted | 3ms | 3660 KiB | ||||
14 | Wrong answer | 4ms | 3928 KiB | ||||
15 | Accepted | 188ms | 24600 KiB | ||||
16 | Wrong answer | 2.421s | 135948 KiB | ||||
17 | Time limit exceeded | 3.068s | 50320 KiB | ||||
18 | Time limit exceeded | 3.082s | 33364 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Accepted | 12ms | 5796 KiB | ||||
20 | Time limit exceeded | 3.107s | 117000 KiB | ||||
21 | Time limit exceeded | 3.075s | 82556 KiB | ||||
22 | Time limit exceeded | 3.081s | 51324 KiB | ||||
23 | Time limit exceeded | 3.063s | 42156 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Accepted | 14ms | 6820 KiB | ||||
25 | Accepted | 14ms | 6948 KiB | ||||
26 | Accepted | 14ms | 6956 KiB | ||||
27 | Accepted | 14ms | 7000 KiB | ||||
28 | Wrong answer | 17ms | 7348 KiB | ||||
29 | Accepted | 18ms | 7484 KiB | ||||
30 | Time limit exceeded | 3.045s | 140364 KiB | ||||
31 | Time limit exceeded | 3.068s | 113008 KiB | ||||
32 | Time limit exceeded | 3.082s | 83852 KiB | ||||
33 | Time limit exceeded | 3.072s | 54156 KiB | ||||
34 | Time limit exceeded | 3.052s | 42708 KiB | ||||
35 | Time limit exceeded | 3.059s | 35064 KiB | ||||
36 | Time limit exceeded | 3.072s | 34080 KiB | ||||
37 | Time limit exceeded | 3.048s | 37708 KiB | ||||
38 | Time limit exceeded | 3.072s | 53944 KiB | ||||
39 | Time limit exceeded | 3.072s | 66872 KiB | ||||
40 | Time limit exceeded | 3.082s | 86968 KiB |