103032024-03-30 13:14:39111Majomházcpp17Time limit exceeded 10/1003.099s6800 KiB
#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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1840 KiB
2Time limit exceeded3.099s1436 KiB
subtask210/10
3Accepted3ms2268 KiB
4Accepted3ms2392 KiB
5Accepted3ms2720 KiB
6Accepted2ms2612 KiB
7Accepted3ms2608 KiB
subtask30/10
8Accepted3ms2620 KiB
9Accepted19ms2632 KiB
10Accepted574ms2628 KiB
11Time limit exceeded3.076s2864 KiB
12Time limit exceeded3.052s3196 KiB
subtask40/20
13Accepted3ms3376 KiB
14Accepted4ms3336 KiB
15Wrong answer72ms3464 KiB
16Time limit exceeded3.056s3444 KiB
17Time limit exceeded3.076s3340 KiB
18Time limit exceeded3.072s2552 KiB
subtask50/29
19Runtime error8ms4236 KiB
20Runtime error8ms4484 KiB
21Runtime error8ms4512 KiB
22Runtime error8ms4508 KiB
23Runtime error8ms4508 KiB
subtask60/31
24Runtime error14ms5508 KiB
25Runtime error14ms5752 KiB
26Runtime error14ms5956 KiB
27Runtime error14ms6244 KiB
28Runtime error14ms6128 KiB
29Runtime error14ms6416 KiB
30Runtime error14ms6676 KiB
31Runtime error14ms6504 KiB
32Runtime error14ms6396 KiB
33Runtime error14ms6400 KiB
34Runtime error14ms6396 KiB
35Runtime error14ms6672 KiB
36Runtime error14ms6508 KiB
37Runtime error14ms6512 KiB
38Runtime error14ms6512 KiB
39Runtime error14ms6636 KiB
40Runtime error14ms6800 KiB