#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=(k-1)/2;
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';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1708 KiB | ||||
2 | Accepted | 1.277s | 116752 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2132 KiB | ||||
4 | Accepted | 3ms | 2344 KiB | ||||
5 | Accepted | 3ms | 2700 KiB | ||||
6 | Accepted | 3ms | 2904 KiB | ||||
7 | Accepted | 3ms | 2808 KiB | ||||
subtask3 | 10/10 | ||||||
8 | Accepted | 3ms | 3112 KiB | ||||
9 | Accepted | 10ms | 4132 KiB | ||||
10 | Accepted | 75ms | 8480 KiB | ||||
11 | Accepted | 319ms | 14560 KiB | ||||
12 | Accepted | 794ms | 20504 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Accepted | 3ms | 3896 KiB | ||||
14 | Accepted | 4ms | 4056 KiB | ||||
15 | Wrong answer | 45ms | 9588 KiB | ||||
16 | Time limit exceeded | 3.062s | 111852 KiB | ||||
17 | Time limit exceeded | 3.102s | 45408 KiB | ||||
18 | Time limit exceeded | 3.065s | 31360 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Accepted | 12ms | 5920 KiB | ||||
20 | Accepted | 435ms | 50444 KiB | ||||
21 | Time limit exceeded | 3.094s | 174024 KiB | ||||
22 | Time limit exceeded | 3.079s | 159148 KiB | ||||
23 | Time limit exceeded | 3.062s | 115268 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Accepted | 14ms | 7340 KiB | ||||
25 | Accepted | 14ms | 7228 KiB | ||||
26 | Accepted | 14ms | 7280 KiB | ||||
27 | Accepted | 16ms | 7232 KiB | ||||
28 | Wrong answer | 17ms | 7660 KiB | ||||
29 | Wrong answer | 19ms | 7852 KiB | ||||
30 | Accepted | 224ms | 31980 KiB | ||||
31 | Time limit exceeded | 3.061s | 185832 KiB | ||||
32 | Time limit exceeded | 3.111s | 191372 KiB | ||||
33 | Time limit exceeded | 3.071s | 162468 KiB | ||||
34 | Time limit exceeded | 3.078s | 156268 KiB | ||||
35 | Time limit exceeded | 3.078s | 73312 KiB | ||||
36 | Time limit exceeded | 3.078s | 31828 KiB | ||||
37 | Time limit exceeded | 3.071s | 31604 KiB | ||||
38 | Time limit exceeded | 3.078s | 35560 KiB | ||||
39 | Time limit exceeded | 3.068s | 38372 KiB | ||||
40 | Time limit exceeded | 3.075s | 47204 KiB |