#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,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;
if(N*K>100000)exit(1);
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 | 1832 KiB | ||||
2 | Accepted | 1.177s | 117568 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2300 KiB | ||||
4 | Accepted | 2ms | 2388 KiB | ||||
5 | Accepted | 3ms | 2412 KiB | ||||
6 | Accepted | 3ms | 2764 KiB | ||||
7 | Accepted | 3ms | 2980 KiB | ||||
subtask3 | 10/10 | ||||||
8 | Accepted | 3ms | 2892 KiB | ||||
9 | Accepted | 9ms | 3888 KiB | ||||
10 | Accepted | 63ms | 8160 KiB | ||||
11 | Accepted | 266ms | 14200 KiB | ||||
12 | Accepted | 689ms | 20136 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Accepted | 3ms | 3348 KiB | ||||
14 | Accepted | 4ms | 3588 KiB | ||||
15 | Accepted | 43ms | 9176 KiB | ||||
16 | Runtime error | 3ms | 3328 KiB | ||||
17 | Runtime error | 2ms | 3268 KiB | ||||
18 | Runtime error | 2ms | 3268 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Runtime error | 2ms | 3268 KiB | ||||
20 | Runtime error | 3ms | 3496 KiB | ||||
21 | Runtime error | 3ms | 3704 KiB | ||||
22 | Runtime error | 3ms | 3692 KiB | ||||
23 | Runtime error | 3ms | 3688 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Accepted | 14ms | 6576 KiB | ||||
25 | Accepted | 16ms | 6876 KiB | ||||
26 | Runtime error | 3ms | 4168 KiB | ||||
27 | Runtime error | 2ms | 4204 KiB | ||||
28 | Runtime error | 3ms | 4208 KiB | ||||
29 | Runtime error | 2ms | 4208 KiB | ||||
30 | Runtime error | 2ms | 4208 KiB | ||||
31 | Runtime error | 2ms | 4208 KiB | ||||
32 | Runtime error | 2ms | 4200 KiB | ||||
33 | Runtime error | 2ms | 4120 KiB | ||||
34 | Runtime error | 2ms | 4208 KiB | ||||
35 | Runtime error | 3ms | 4252 KiB | ||||
36 | Runtime error | 2ms | 4132 KiB | ||||
37 | Runtime error | 2ms | 4220 KiB | ||||
38 | Runtime error | 2ms | 4132 KiB | ||||
39 | Runtime error | 2ms | 4128 KiB | ||||
40 | Runtime error | 2ms | 4220 KiB |