#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
int pf[5001],dp[5001][5001];
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);
}
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));
}
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 | 1840 KiB | ||||
2 | Time limit exceeded | 3.099s | 1436 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2268 KiB | ||||
4 | Accepted | 3ms | 2392 KiB | ||||
5 | Accepted | 3ms | 2720 KiB | ||||
6 | Accepted | 2ms | 2612 KiB | ||||
7 | Accepted | 3ms | 2608 KiB | ||||
subtask3 | 0/10 | ||||||
8 | Accepted | 3ms | 2620 KiB | ||||
9 | Accepted | 19ms | 2632 KiB | ||||
10 | Accepted | 574ms | 2628 KiB | ||||
11 | Time limit exceeded | 3.076s | 2864 KiB | ||||
12 | Time limit exceeded | 3.052s | 3196 KiB | ||||
subtask4 | 0/20 | ||||||
13 | Accepted | 3ms | 3376 KiB | ||||
14 | Accepted | 4ms | 3336 KiB | ||||
15 | Wrong answer | 72ms | 3464 KiB | ||||
16 | Time limit exceeded | 3.056s | 3444 KiB | ||||
17 | Time limit exceeded | 3.076s | 3340 KiB | ||||
18 | Time limit exceeded | 3.072s | 2552 KiB | ||||
subtask5 | 0/29 | ||||||
19 | Runtime error | 8ms | 4236 KiB | ||||
20 | Runtime error | 8ms | 4484 KiB | ||||
21 | Runtime error | 8ms | 4512 KiB | ||||
22 | Runtime error | 8ms | 4508 KiB | ||||
23 | Runtime error | 8ms | 4508 KiB | ||||
subtask6 | 0/31 | ||||||
24 | Runtime error | 14ms | 5508 KiB | ||||
25 | Runtime error | 14ms | 5752 KiB | ||||
26 | Runtime error | 14ms | 5956 KiB | ||||
27 | Runtime error | 14ms | 6244 KiB | ||||
28 | Runtime error | 14ms | 6128 KiB | ||||
29 | Runtime error | 14ms | 6416 KiB | ||||
30 | Runtime error | 14ms | 6676 KiB | ||||
31 | Runtime error | 14ms | 6504 KiB | ||||
32 | Runtime error | 14ms | 6396 KiB | ||||
33 | Runtime error | 14ms | 6400 KiB | ||||
34 | Runtime error | 14ms | 6396 KiB | ||||
35 | Runtime error | 14ms | 6672 KiB | ||||
36 | Runtime error | 14ms | 6508 KiB | ||||
37 | Runtime error | 14ms | 6512 KiB | ||||
38 | Runtime error | 14ms | 6512 KiB | ||||
39 | Runtime error | 14ms | 6636 KiB | ||||
40 | Runtime error | 14ms | 6800 KiB |